Static Class In C#

C# static keyword is used to create a static class. A static class in C# is a class that cannot be instantiated. A static class can only contain static data members including static methods, static constructors, and static properties.

C# Static Class

 
A C# static class is a class that can't be instantiated. The sole purpose of the class is to provide blueprints of its inherited classes. A static class is created using the "static" keyword in C#. A static class can contain static members only. You can‘t create an object for the static class.

Advantages of Static Classes

  1. If you declare any member as a non-static member, you will get an error. 
  2. When you try to create an instance to the static class, it again generates a compile time error, because the static members can be accessed directly with its class name.
  3. The static keyword is used before the class keyword in a class definition to declare a static class.
  4. A static class members are accessed by the class name followed by the member name. 

Syntax of static class

  1. static class classname  
  2. {  
  3.    //static data members  
  4.    //static methods  
  5. }  
Static Class Demo
  1. namespace StaticConstructorsDemo  
  2. {  
  3.     class MyCollege  
  4.     {  
  5.         //static fields  
  6.         public static string CollegeName;  
  7.         public static string Address;  
  8.   
  9.         //static constructor  
  10.         static MyCollege()  
  11.         {  
  12.             CollegeName = "ABC College of Technology";  
  13.             Address = "Hyderabad";  
  14.         }  
  15.     }  
  16.     class Program  
  17.     {  
  18.         static void Main(string[] args)  
  19.         {  
  20.             Console.WriteLine(MyCollege.CollegeName);  
  21.             Console.WriteLine(MyCollege.Address);  
  22.             Console.Read();  
  23.         }  
  24.     }  
  25. } 

Static Members


There are two types of C# static class members, static and non-static.
 
Non-static members
 
This is the default type for all the members. If you do not use the "static" keyword for the declaration of a field / property or a method, then it can be called a "Non-static member". The main feature of a non-static member is it will be bound with the object only.

Non-static Fields / Properties
The memory is allocated when the object is created.

Non-static Methods
These methods can implement operations on non-static fields and properties

Static Members
 

If you use the "static" keyword for the declaration of a field / property or a method, then it is called a "Static member". The main feature of a non-static member is that it will not be bound with any object. It is individually accessible with the class name. In other words, the static members are accessible directly, without even creating one object also.

Static Fields / Properties
The memory will be allocated individually, without any relation with the object.

Static Methods
These methods can implement operations on static fields and properties only; and can‘t access the non-static members.

Static Members 

Demo on static members
  1. using System;  
  2. using System.Linq;  
  3. using System.Text;  
  4. using System.Collections.Generic;  
  5. namespace StaticConstructorsDemo  
  6. {  
  7.     class Student  
  8.     {  
  9.         //non-statcic data members  
  10.         public string StudentName;  
  11.         public string Course;  
  12.         public void SetStudentDetails(string StuName, string Cou)  
  13.         {  
  14.             StudentName = StuName;  
  15.             Course = Cou;  
  16.         }  
  17.         public void DisplayStudentDetails()  
  18.         {  
  19.             Console.WriteLine(StudentName + " - " + Course);  
  20.         }  
  21.   
  22.         //static data members  
  23.         public static string CollegeName = "ABC College of Technology";  
  24.         public static string CollegeAddress = "Hyderabad";  
  25.   
  26.         //static methods  
  27.         public static void DisplayCollegeDetails()  
  28.         {  
  29.             Console.WriteLine(CollegeName);  
  30.             Console.WriteLine(CollegeAddress);  
  31.         }  
  32.     }  
  33.     class Program  
  34.     {  
  35.         static void Main(string[] args)  
  36.         {  
  37.             //access static members  
  38.             Student.DisplayCollegeDetails();  
  39.   
  40.             //acess non-static members  
  41.             Console.WriteLine();  
  42.             Student s1= new Student();  
  43.             Student s2 = new Student();  
  44.             s1.SetStudentDetails("Sarath","MCA");  
  45.             s1.SetStudentDetails("Syam","MBA");  
  46.             s1.DisplayStudentDetails();  
  47.             s2.DisplayStudentDetails();  
  48.             Console.Read();  
  49.         }  
  50.     }  
  51. }

 
Static Constructors

A static constructor is used to initialize the static data members, whereas the normal constructor (non-static constructor) is used to initialize the non-static data members.

Syntax

  1. static classname()  
  2. {  
  3.    //some code  
  4. }  

Rules

  1. Static constructors can‘t contain any access modifiers.
  2. Static constructors can‘t be defined with arguments.
  3. Static constructors can‘t access the non-static data members.
Demo on static constructors
  1. namespace StaticConstructorsDemo  
  2. {  
  3.     class MyCollege  
  4.     {  
  5.         //static fields  
  6.         public static string CollegeName;  
  7.         public static string Address;  
  8.   
  9.         //static constructor  
  10.         static MyCollege()  
  11.         {  
  12.             CollegeName = "ABC College of Technology";  
  13.             Address = "Hyderabad";  
  14.         }  
  15.     }  
  16.     class Program  
  17.     {  
  18.         static void Main(string[] args)  
  19.         {  
  20.             Console.WriteLine(MyCollege.CollegeName);  
  21.             Console.WriteLine(MyCollege.Address);  
  22.             Console.Read();  
  23.         }  
  24.     }  
  25. } 

Summary

 
In this article, we saw how to create and use a static class in C#.