Static Classes In C#

Introduction

If we normally define static class; it is a class that cannot be instantiated. In other words, you can say class whose object cannot be created or a class where a new keyword cannot be used to create a variable of the class. The members of static class can be accessed by using the class name itself. We don’t need to make an instance of the class. A static class can only contain static members. Static classes are automatically loaded by .NET Framework CLR.

Declaration

A static class can be created, using the keyword “Static”.

e.g.

  1. static class MyClass  
  2.   {  
  3.   
  4.   }  

In the code given above, class with name MyClass is static as Static keyword is used before class.

Features of static class 

  1.  Static class contains only static members.
  2.  It cannot be instantiated.
  3.  Static class is always sealed.

Understanding Static Class

Methods in static class are accessed, using the class name.

e.g.

  1.   static class MyClass  
  2.     {  
  3.         public static void MyFunc()  
  4.         {  
  5.             Console.WriteLine("Method from static class");  
  6.         }  
  7.     }  
  8.   
  9. //the method MyFunc  cannot be called by creating the instance of the class “MyClass”.  
  10. //It can be called as:  
  11. static void Main(string[] args)  
  12.         {  
  13.             MyClass.MyFunc();   //calling method with class name  
  14.             Console.ReadLine();  
  15.         }   

Creating an Instance of Static class

As I already said Static class cannot be instantiated, let us see this in an example by trying to create the instance of Static class.

  1. class Program  
  2.   {  
  3.       static void Main(string[] args)  
  4.       {  
  5.           MyClass mc = new MyClass();  // trying to create the instance of the static class  
  6.   
  7.           // MyClass.MyFunc();  
  8.           // Console.ReadLine();  
  9.       }  
  10.   }  
  11.   
  12.   static class MyClass  
  13.   {  
  14.       public static void MyFunc()  
  15.       {  
  16.           Console.WriteLine("Method from static class");  
  17.       }  
  18.   }  

While ignoring the warning and trying to run the program, we get this error.

 

Now, we will create a static class with one sum function and will call the sum function, using class name.

MySumFunc function has two input parameters for the sum. We will provide two input parameters to the function , while calling it to check the output.
  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         int result;  
  6.         result = MyClass.MySumFunc(3, 4);  
  7.         Console.WriteLine(result);  
  8.         Console.ReadLine();  
  9.     }  
  10. }  
  11.   
  12. static class MyClass  
  13. {  
  14.     public static int MySumFunc(int a, int b)  
  15.     {  
  16.         return a + b;  
  17.     }  
  18. }  

After running the program , we will get the desired result, 7 (as we are providing 3 and 4 to sumFunc).

Ebook Download
View all
Learn
View all