Static Members in C#

I just want explain the concept of static with real world example in a simple way.

Let’s create Student class as below,

  1. class Student  
  2. {  
  3.     public int StudentId  
  4.     {  
  5.         get;  
  6.         set;  
  7.     }  
  8.     public string StudentName  
  9.     {  
  10.         get;  
  11.         set;  
  12.     }  
  13.     public string StudentClassName  
  14.     {  
  15.         get;  
  16.         set;  
  17.     }  
  18.     public string HeadMasterName  
  19.     {  
  20.         get;  
  21.         set;  
  22.     }  
  23. }  
In the above student class, we have four data members like below,

  • StudentId - Represent student registration number\
  • StudentName - Represent student name
  • StudentClassName - Represent student class in which he/she studying
  • HeadMasterName - Represent Head Master of the student

We create the individual student by using following function.

  1. public void getStudentInfo()  
  2. {  
  3.     Console.WriteLine("Enter the student details(StudentId,StudentName,StudentClassName,HeadMasterName)");  
  4.     StudentId = Convert.ToInt32(Console.ReadLine());  
  5.     StudentName = Console.ReadLine().ToString();  
  6.     StudentClassName = Console.ReadLine().ToString();  
  7.     HeadMasterName = Console.ReadLine().ToString();  
  8. }  
Guys, we need to think on a point Head Master, For all students the Head Master is same, so is there any need to enter the Head Master whenever we create student.

No need to enter the Head Master details along with every student, because Head Master is common for all students.

So we enter a Head Master name only once and use for all the students i.e Sharing of the Head Master name among all created student objects, come we will do that as below.

First we make Head Master as “static” by adding static keyword behind data member as below.
  1. class Student  
  2. {  
  3.     public int StudentId  
  4.     {  
  5.         get;  
  6.         set;  
  7.     }  
  8.     public string StudentName  
  9.     {  
  10.         get;  
  11.         set;  
  12.     }  
  13.     public string StudentClassName  
  14.     {  
  15.         get;  
  16.         set;  
  17.     }  
  18.     public static string HeadMasterName  
  19.     {  
  20.         get;  
  21.         set;  
  22.     }  
  23. }  
Assign the static variable HeadMaster as below,
  1. public static void Main()  
  2. {  
  3.     Student _studentObject = new Student();  
  4.     _studentObject.getStudentInfo();  
  5.     Student.HeadMasterName = "Hanumanthappa B.K";  
  6.     _studentObject.printStudentInfo();  
  7.     Console.ReadLine();  
  8. }  
By this way we made HeadMaster variable as sharable among all student objects.

Following points to be noticed.

Accessing of static variable with class name not with object name in above example we accessed with class name Student.

If you want to reassign the static variable, you can do it by using class name or in any class function.

Like static variables there is static class, static methods, static constructor.

Conclusion

Static nothing but “SHARING A VARIABLE BETWEEN OBJECTS”.