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 article 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
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 blockClass Demo { int x, static int a; static void Display() { int b = 20; //Static variable Console.WriteLine(a + ” ” +b); } Static void Main() { Console.writeLine(a); Display(); } }
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.

Rajeev PunhaniPosted Mar 29, 2016, 1:37 AM
Nice.
Vignesh ManiPosted Mar 26, 2016, 8:59 AM
good start