Code Examples of C# Static

This blog shows how to use the static keyword in C#. The static keyword is used to create static classes and static members in C#.

Static Keyword


Static keyword can be used in 4 scenarios. 

For Data Members


It is a variable which belongs to the Class. A single copy can be shared by all instances of a class. For the used of static variables, creation of instance is not required.

Access using ClassName.VariableName , unlike instance variables which are accessed as ObjectName.VariableName.

Static Data Member - > StaticDataMember.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace StaticKeyWordDemo.Classes {  
  7.     public class StaticDataMember {  
  8.         private int StudId;  
  9.         private static int nextStudId = 1;  
  10.         public StaticDataMember() {  
  11.             StudId = nextStudId++;  
  12.         }  
  13.         public void PrintRecord() {  
  14.             Console.WriteLine("Student Id is {0}", StudId);  
  15.         }  
  16.     }  
  17. }  
  18. using StaticKeyWordDemo.Classes;  
  19. using System;  
  20. using System.Collections.Generic;  
  21. using System.Linq;  
  22. using System.Text;  
  23. using System.Threading.Tasks;  
  24. namespace StaticKeyWordDemo {  
  25.     class StaticKeyword {  
  26.         static void Main(string[] args) {  
  27.             StaticDataMember sd1 = new StaticDataMember();  
  28.             StaticDataMember sd2 = new StaticDataMember();  
  29.             //printing  
  30.             Console.WriteLine("*** Static Keyword Demo ***");  
  31.             sd1.PrintRecord();  
  32.             sd2.PrintRecord();  
  33.             Console.ReadKey();  
  34.         }  
  35.     }  

Output

Image-1.jpg

Static Method Member


Like any static member, static method belongs to the class (not object). Accessed using ClassName.MethodName, it is a class method. For using static methods, Creation of instance is not necessary. A static method only accesses other static data and methods. Static methods can ot access data members and members method directly – they must use an object reference.

Static Data Members - > StaticMethodMember.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace StaticKeyWordDemo.Classes {  
  7.     public class StaticMethodMember {  
  8.         private int EmpId;  
  9.         private static int nextEmpId = 100;  
  10.         public static void PrintNextEmp() {  
  11.             Console.WriteLine("Next Employee Id is {0}", nextEmpId);  
  12.         }  
  13.     }  
  14. }  
  15. using StaticKeyWordDemo.Classes;  
  16. using System;  
  17. using System.Collections.Generic;  
  18. using System.Linq;  
  19. using System.Text;  
  20. using System.Threading.Tasks;  
  21. namespace StaticKeyWordDemo {  
  22.     class StaticKeyword {  
  23.         static void Main(string[] args) {  
  24.             Console.WriteLine("*** Static Keyword Demo ***");  
  25.             StaticMethodMember.PrintNextEmp();  
  26.             Console.ReadKey();  
  27.         }  
  28.     }  
  29. }   

Output

Image-2.jpg

Static Constructor


A class can have only one static member. Static constructor can not have any parameter. Static constructor cannot have any access specifier, not even private. It is used to initialize the static data members of the class. For any number of object creation, the static constructor gets executed only once.

The static constructor gets excuted when the class is used or referenced for the very first time in the application. It cannot be invoked by the programmer explicitly.

Static Constructor - > StaticConstructor.cs 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace StaticKeyWordDemo.Classes {  
  7.     public class StaticConstructor {  
  8.         private static int nextEmpId;  
  9.         private int EmpId;  
  10.         private string EmpName = "Krishn";  
  11.         static StaticConstructor() {  
  12.             nextEmpId = 100;  
  13.         }  
  14.         public StaticConstructor() {  
  15.             EmpId = nextEmpId++;  
  16.         }  
  17.         public StaticConstructor(string empName) {  
  18.             EmpId = nextEmpId++;  
  19.             this.EmpName = empName;  
  20.         }  
  21.         public void PrintInfo() {  
  22.             Console.WriteLine("EmpId : {0}", EmpId);  
  23.             Console.WriteLine("EmpName : {0}", EmpName);  
  24.         }  
  25.     }  
  26. }  
  27. using StaticKeyWordDemo.Classes;  
  28. using System;  
  29. using System.Collections.Generic;  
  30. using System.Linq;  
  31. using System.Text;  
  32. using System.Threading.Tasks;  
  33. namespace StaticKeyWordDemo {  
  34.     class StaticKeyword {  
  35.         static void Main(string[] args) {  
  36.             Console.WriteLine("*** Static Keyword Demo ***");  
  37.             StaticConstructor sc1 = new StaticConstructor();  
  38.             sc1.PrintInfo();  
  39.             Console.WriteLine();  
  40.             StaticConstructor sc2 = new StaticConstructor("Jeetendra");  
  41.             sc2.PrintInfo();  
  42.             Console.ReadKey();  
  43.         }  
  44.     }  

Output

Image-3.jpg
 
The first time during class load for execution, the nexEmpId gets initialized to 100. After that every instance of StaticConstructor class is created using either of the constructors, the nextEmpId is assigned to the instance field empId and then incremented. Thus this code generates a unique number stating from 100 and assigns it to each individual StaticConstructor object as its empId.

Static constructor is called implicitly only once in the lifetime of the application. Subsequently only the default or parameterized constructor is called, depending on how the object is created.

Static Classes


A static class is a class that can't be instantiated. All class members including methods, properties, and fields are static. The only way to access class members is by using the class name. 
 
Read Static Class In C# to learn more about static classes.