| AS the name suggest static Static modifier are used to declare a static member and belongs to type itself rather than to specific object. We can use static modifier with fields, operators, properties, events, constructors and methods but we cannot used with indexers, destructors, or types.Once we declare static varible it will automatically initialized in memory by there default value or value defined by user once we will initialized static variable it will never reinitialized in memory on other hand local variables local variable reinitialized in memory every time they call as well as all the objects of the class share the same address of static variable.
In C# the static keyword indicates a class variable. In VB the equivalent keyword is Shared. Its scoped to the class in which it occurs.
Below is the example to understand what static is and where to use static
class TestStatic
{
static int a = b;
static int b = 1;
static void Main()
{
Console.WriteLine(TestStatic.a);
Console.WriteLine(TestStatic.b);
TestStatic.a = 2;
Console.WriteLine(TestStatic.a);
}
}
-----Output-------
0 ///by default it will take 0 value of a
1
2 | | |