Static Vs Instance And Constant Vs Readonly Variable In C#

This concept which I am going to discuss is a very basic concept, yet often a confusing concept for beginners, and you will find many articles about this topic on the internet; but today I am going to discuss some new things.

This articles gives you the best knowledge about this topic. Read it, I believe definitely you will get something new.

Before going on to discuss our topic let’s first understand what is means by static member and instance member.

In C# all the data members which can be initialized at the time of object creation are called Instance member and the members which aren't required objects to initialize the variable are called Static member.

Now let’s starts with Static variable Vs Instance variable.

  • By default every variable under a class is Instance variable unless it is declared by using a static modifier or declared under static block.

    Example:
    1. Class Demo  
    2. {  
    3.     int x,  
    4.      
    5.     static int a;  
    6.     static void Display()  
    7.     {  
    8.         int b = 20; //Static variable  
    9.         Console.WriteLine(a + ” ” +b);  
    10.     }  
    11.     Static void Main()  
    12.     {  
    13.         Console.writeLine(a);  
    14.         Display();  
    15.      
    16.     }  
    17. }  
    In the above example I have declared variable ‘a’ as static ,so this variable got Initialized by it’s default value when the class loading happen and here b is also a static variable because it's declared under a static block 

    Key Capsule:
    Here important thing to remember is that when we are using static modifier to any of the variable that must be initialized only once at the class loading time and the variable if declared under static block then that variable also a static variable.

Static VS Instance Variable

  • A static variable of class get initialized immediately once the execution of class starts or we say at the class loading time, whereas an instance variable get initialized only after creating the object of that class.

    Key Capsule: It’s important to remember that when we call the Constructor of that class using new keyword at that moment only the instance variable got initialized .The number or time we call the constructor as that many time the variable got Initialized.

  • In the life cycle of class a static variables get initialized one and only once while class being loading into memory where as an instance variable get initialized each and every time the object is created.

    Interview Capsule: In many interview asking that what is the min and max count of Initialization for static and instance variable?

    Answer: The min count and max count is always one for the Static variable because it got initialized immorally when the class loaded inside the memory and persist in that value through its life cycle, but the min count for instance variable is always zero because if we don’t created any object for that class then the variable can not got initialized and max count is ‘n’, hear ‘n’ represent how many time we created the object that many time the instance variable got initialized.

  • To consume the static member outside of the class we need class name whereas to access the instance member outside of the class we need object of that class.

Constant variable VS Read only Variable

  • A variable where value can’t be modified once after declaration is know as constant variable where as in read only variable we can make separation in between declaration and initialization.

    For eg: If I do like this,
    1. Const int x; //Error  
    2. X=9; //Wrong  
    3. Readonly int x; //No Error  
    4. X=9; //Correct.  
    Similar behavior in between constant variable and a static variable.

    Like static variable initialize immediately when the class loads inside memory, in the same way constant variable got initialized only once at the class loading time.

    Where as we have also similar behavior for readonly variable and instance variable.

  • To declare constant variable we use ‘const’ keyword where as to declare readonly variable we use ‘readonly’ keyword.

    Example:
    1. const float pi=3.14f; //No error  
    2. rreadonly int a=89; //No error  
    3. Or readonly int a;  
    4. a=89;  

Readonly

  1. Key Capsule: It’s important to remember is that the readonly variable can declared and initialized at one line or we separate it as well as we can initialize readonly variable through constructor of that class.

  2. Key Capsule: The value of readonly variable is constant for a single object that means the readonly variable value fix with in the life cycle of an object. So hear important part is to understand that if create more then one object then we can make change the readonly variable value by using particular object.

    For e.g:
    1. Class Demo  
    2. {  
    3.     readonly int a;  
    4.     readonly float pi;  
    5.     Demo(int a)  
    6.     {  
    7.         this.a = a;  
    8.     }  
    9.     Static void Main()  
    10.     {  
    11.         Demo obj1 = new Demo(8);  
    12.         Console.writeLIne(obj1.a)  
    13.         Obj1.pi = 3.141 f;  
    14.         Obj1.pi = 2.14 f; //Error  
    15.         Console.writeLIne(obj1.pi);  
    16.         Demo obj2 = new Demo(10);  
    17.         Console.writeLine(obj2.a);  
    18.         Obj2.pi = 1.12 f;  
    19.         Obj2.pi = 6.90 f; //Error  
    20.         Console.writeLIne(obj2.pi)  
    21.     }  
    22. }  
    Output:

    8, 3.141,10,1.12.

So in the example what we see is that readonly variable makes the value fixed  for a single object, as many numbers of of object we created that many times we can re-initialize the readonly variable.

Memory Capsule for Static Class:

  • Static class only contains static member.
  • Inside static class we don’t have any parameterized constructor.
  • Static class is a sealed class that means we can’t inherit any class form static class.
  • Static constructor is the first block of code which executed before the Main method execution.

I hope this article will help your understanding of static, instance, readonly and constant variable.

If you are a fresher then I must suggest you please carefully read all the “Memory Capsule” and “KeyCapusle” which will help you a lot in your interview.

Never accept more as your capability; take all your time to improve your knowledge and confidence so that without accepting more by default you get what you want”.

Read more articles on C# Programming: