Static is a keyword in C# that is when used with different to declare static classes and static class members. Static can be field, method, constructor, class, properties, operator, and event. The instance of a class is not required in order to access static members.
There is only one copy of the object is created for the static members and is shared among all the objects. Let see the use of static in three areas of the program:
- static field
- static method
- static class
C# Static Variable or field
The variable or field in class when declared static is called static field. The difference between static and instance fields is, no matter how many times you create an object for the class, there will be only one memory allocated for the static field of the class and is shared by all the objects.
But in the case of the instance field, with the creation of every object of the class, a new memory is allocated for the instance field for each object.
Example: C# example for the static field.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | using System; class StaticExample { public static int temp; public void counter() { temp++; } public int getTemp() { return temp; } } class StaticMain { static void Main(string[] args) { StaticExample st1 = new StaticExample(); StaticExample st2 = new StaticExample(); st1.counter(); st1.counter(); st2.counter(); st2.counter(); Console.WriteLine("Temp count for st1: {0}", st1.getTemp()); Console.WriteLine("Temp count for st2: {0}", st2.getTemp()); } } |
Output:
Temp count for st1: 4
Temp count for st2: 4
As you can see, no matter which object you use to call, the temp keeps increasing. This indicated that the static field has only one instance and is used by all the other objects.
C# Static Methods
The methods that are declared with a static keyword are called static methods. This method can only access the static field of the class.
Just like a static field, the static function also does not depend on the object of the class. Let us see an example.
Example: C# example for the static method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | using System; class StaticExample { public static int temp; public void counter() { temp++; } public static int getTemp() { return temp; } } class StaticMain { static void Main(string[] args) { StaticExample st = new StaticExample(); st.counter(); st.counter(); Console.WriteLine("Temp count for st: {0}", StaticExample.getTemp()); } } |
Output:
Temp count for st: 2
Also, note that the static method can not be invoked with the object of the class. The class name itself is used to invoke the static method. In the above eg. StaticExample.getTemp()
is used to invoke the static method getTemp()
.
C# static class
The static class in C# is cannot be instantiated. It can only have static members in its body. And it is created using a static keyword in front of the class like the static field and method.
Following are some points to remember about static class.
- The instance of static class cannot be created.
- To access static members, a class name is used which is followed by the member name.
- It only has static members and canoot be intatiated.
Syntax of static class
1 2 3 4 5 | public static class class_name { //static data members //static methods } |
Example: C# example for the static class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | using System; namespace Program { public static class Square { //static fields public static int side = 10; //static method public static void area() { Console.WriteLine("Area of a square: " + side *side); } } class StaticMain { static void Main(string[] args) { Console.WriteLine("Side of a square: " + Square.side); Square.area(); } } } |
Output:
Side of a square: 10
Area of a square: 100