Static in C#:
Static is a compiler reserved keyword in C#, which can be applied to any member of a class i.e. well Fields, methods, classes, Properties, Events or to a class itself as well.
There will be only one copy of a member variable declared as static. Each object has its own copy of instance variables whereas static variables are shared across objects.
Lifetime:
Static variables are allocated when type containing the static variables is loaded in memory and are unallocated when the class is removed from memory.
Storage:
Static variables, Static Methods as well as normal methods are stored in a special area of heap called “PermGen”. PermGen stores only primitive values of static variables, if static variable is a reference type than the reference variable will be in PermGen whereas the actual object will be allocated in Heap.
Usage:
System.Console.WriteLine(“Hello Static”);
C# 6.0
There is short and concise way of calling static methods
using System;
using System.Console; //Importing System.Console will import all static methods in //current namespace
namespace CSharp6
{
class MyClass
{
public void Hello()
{
WriteLine("Hello Static");
}
}
}