Instance Variable vs static variable
Eg:
int x=100;
Static int y=200;
Static void Main()
{
Int z=300;
}
Note: here the instance variable gets initialized whenever the object of class gets created, so the minimum or maximum no of times, it gets initialized will be zero or N.
Whereas as a static variable get initialized once the execution of a class starts. so the minimum and the maximum no of times it gets initialized will be one and only one.
Use instance variable if we want the variable accessible only to the single object
Statvar.cs
Class statvars
{
int x=100;
static int y=200;
static void Main()
{
Console.writeline(statvar.y);
Statvar sv=new statvar();
Console.writeline(sv.x);
Console.readline();
}
No comments:
Post a Comment
Thank you for visiting my blog