You must Sign In to post a response.
  • Category: Visual Studio

    what is the difference between static and readonly in C#.net

    i want difference between static keyword and readonly please help me
  • #746650
    Readonly is the keyword and we can change its value during runtime but only through the non-static constructor. Not even a method.

    Static word is used to define a static members means it can be common to all the objects and they do not tied to a specific object. if we make a static class then all the members of the class must be static. if we make a any variable static then it can accessed without class or form object
    for more information on static go through following link
    http://www.dotnet-tricks.com/Tutorial/csharp/FU4N141113-Difference-Between-Constant-and-ReadOnly-and-Static.html

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #746652
    you can change the value of a static keyword but you cannot change the value of read only field once you assign it a value. Static variable can be shared across multiple users and changed as per the requirement but readonly variable cannot be changed.
    static variable is accessed using classname.variablename whereas readonly variable is accessed using object of the class.

    Miss. Jain
    Microsoft Certified Technology Specialist in .Net

  • #746659
    Hi,

    As its name suggests if you declare a variable as readonly the variable cannot be changed in all the places except in the constructor.
    The advantage of readonly is you will be able to assign the value once unlike constant which you need to assign in advance.
    There is one good explanation in this article: dotnetperls.com/readonly

    In case of normal variable you can assign and reassign at any time you want. There is no restriction on this.

    Static class is similar to other classes but you don't need to instantiate static class. Which means you don't need to create object of the class to access the methods.
    System.Math class is one of the static class in .NET.

    You may read more about it from MSDN: msdn.microsoft.com/en-us/library/79b3xss3.aspx


    Regards,
    Asheej T K


  • Sign In to post your comments