STATIC VARIABLE
Static Variable:
Static variables are allocated storage from static memory when the program is first loaded into memory. Their life time is the life of the program. Variables declared with the Static keyword remain in existence and retain their latest values after termination of the procedure in which they are declared.
Characteristics:
A static variable comes into existence before execution of the static constructor for its containing type, and ceases to exist when the associated application domain ceases to exist.
Limitations:
Since static variables are basically the same thing as global variables in other languages, and globals are generally a bad idea. This is especially true in a server side application where you have lots of threads going at the same time. There is posibilities for situations where a user can get the data from another's request, all because of a, seemingly, innocuous static variable.
Why Static Variables cant be accesed with object?
If we take a normal variable say "int n",For every instance a new memory will be allocated.But in "static int n" there will be only one allocation that is shared by all ie they are not associated with a particular instance and they retain their latest values.
Example:
using System;
namespace Consolestatic
{
class StaticClass
{
public static int m;
public int n;
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
StaticClass o1=new StaticClass();
StaticClass.m=10;
o1.n=10;
StaticClass o2=new StaticClass();
o2.n=20;
StaticClass.m=20;
Console.WriteLine("First Instance o1.n="+o1.n.ToString());
Console.WriteLine("Second Instance o2.n="+o2.n.ToString());
Console.WriteLine("Static Variable StaticClass.m="+StaticClass.m.ToString());
Console.Read();
}
}
}
OUTPUT:
First Instance o1.n=10
Second Instance o2.n=20
Static Variable StaticClass.m=20
Declaring static variable inside a function in C#:
This will normally gives a error
C:\babu\Checking Projects\Consolestatic\Class1.cs(14): Invalid expression term 'static'
The Reason behind this is methods cant maintain their states between invocations...
So we Should declare the static variables inside the class but not inside a function.
Static variables are allocated storage from static memory when the program is first loaded into memory. Their life time is the life of the program. Variables declared with the Static keyword remain in existence and retain their latest values after termination of the procedure in which they are declared.
Characteristics:
A static variable comes into existence before execution of the static constructor for its containing type, and ceases to exist when the associated application domain ceases to exist.
Limitations:
Since static variables are basically the same thing as global variables in other languages, and globals are generally a bad idea. This is especially true in a server side application where you have lots of threads going at the same time. There is posibilities for situations where a user can get the data from another's request, all because of a, seemingly, innocuous static variable.
Why Static Variables cant be accesed with object?
If we take a normal variable say "int n",For every instance a new memory will be allocated.But in "static int n" there will be only one allocation that is shared by all ie they are not associated with a particular instance and they retain their latest values.
Example:
using System;
namespace Consolestatic
{
class StaticClass
{
public static int m;
public int n;
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
StaticClass o1=new StaticClass();
StaticClass.m=10;
o1.n=10;
StaticClass o2=new StaticClass();
o2.n=20;
StaticClass.m=20;
Console.WriteLine("First Instance o1.n="+o1.n.ToString());
Console.WriteLine("Second Instance o2.n="+o2.n.ToString());
Console.WriteLine("Static Variable StaticClass.m="+StaticClass.m.ToString());
Console.Read();
}
}
}
OUTPUT:
First Instance o1.n=10
Second Instance o2.n=20
Static Variable StaticClass.m=20
Declaring static variable inside a function in C#:
This will normally gives a error
C:\babu\Checking Projects\Consolestatic\Class1.cs(14): Invalid expression term 'static'
The Reason behind this is methods cant maintain their states between invocations...
So we Should declare the static variables inside the class but not inside a function.



