static is a modifier in C# which is applicable for the following:
It is also applicable to properties, event, and operators. To create a static member(class, variable, methods, constructor), precede its declaration with the keyword static. When a member is declared static, it can be accessed with the name of its class directly.
Static Class
A static class is declared with the help of static keyword. A static class can only contain static data members, static methods, and a static constructor. It is not allowed to create objects of the static class. Static classes are sealed, means one cannot inherit a static class from another class.
Example:
C#
using System;
static class Tutorial {
public static string Topic = "Static class" ;
}
public class GFG {
static public void Main()
{
Console.WriteLine( "Topic name is : {0} " , Tutorial.Topic);
}
}
|
Output:
Topic name is : Static class
Static Variable
A static variable is declared with the help of static keyword. When a variable is declared as static, then a single copy of the variable is created and shared among all objects at the class level. Static variables are accessed with the name of the class, they do not require any object for access.
Example:
C#
using System;
class Vehicle {
public static string Model_color = "Black" ;
}
public class GFG {
static public void Main()
{
Console.WriteLine( "Color of XY model is : {0} " ,
Vehicle.Model_color);
}
}
|
Output:
Color of XY model is : Black
Static Method
A static method is declared with the help of static keyword. Static methods are accessed with the name of the class. A static method can access static and non-static fields, static fields are directly accessed by the static method without class name whereas non-static fields require objects.
Example:
C#
using System;
class Nparks {
static public int t = 104;
public static void total()
{
Console.WriteLine( "Total number of national parks" +
" present in India is :{0}" , t);
}
}
public class GFG {
static public void Main()
{
Nparks.total();
}
}
|
Output:
Total number of national parks present in India is :104
Static Constructor
A static constructor is declared with the help of static keyword. Static Constructor has to be invoked only once in the class and it has been invoked during the creation of the first reference to a static member in the class. A static constructor is initialized static fields or data of the class and to be executed only once.
Points To Remember:
- It can’t be called directly.
- When it is executing, then the user has no control.
- It does not take access modifiers or any parameters.
- It is called automatically to initialize the class before the first instance created.
Example:
C#
using System;
class G1 {
static G1()
{
Console.WriteLine( "Example of Static Constructor" );
}
public G1( int j)
{
Console.WriteLine( "Instance Constructor " + j);
}
public string g1_detail( string name, string branch)
{
return "Name: " + name + " Branch: " + branch;
}
public static void Main()
{
G1 obj = new G1(1);
Console.WriteLine(obj.g1_detail( "Sunil" , "CSE" ));
G1 ob = new G1(2);
Console.WriteLine(ob.g1_detail( "Sweta" , "ECE" ));
}
}
|
Output:
Example of Static Constructor
Instance Constructor 1
Name: Sunil Branch: CSE
Instance Constructor 2
Name: Sweta Branch: ECE
Limitation of using static keyword:
- static keyword cannot be used by indexers, finalizers, or types other than classes.
- A static member is not referenced through an instance.
- In C#, it is not allowed to use this to reference static methods or property accessors.
- In C#, if static keyword is used with the class, then the static class always contain static members.