Static Classes in C#

Background

In this article we will learn about one of the most reusable object oriented features of C#, static classes. We will learn about them from the basics because I have written this article focusing on students and beginners. Before proceeding further, please refer to my previous articles for a better understanding.

So, let us start from the basics of static classes.

What Static Classes Are
 
They are a type of class that cannot be initiated, in other words we cannot create an object of that class using the new keyword, such as class members can be called directly by using their class name.
 
The following are some key points:
  • Created using the static keyword.
  • Inside a static class only static members are allowed, in other words everything inside the static class must be static.
  • We cannot create an object of a static class.
  • A static class cannot be inherited.
  • It allows only a static constructor to be declared.
  • The methods of a static class can be called using the class name without creating an instance.
Syntax
  1. [Access Specifier] [Static Keyword] [Class Template] [ Class Name]  
  2.   
  3. //class defination 
 Example
  1. Public Static Class Customer  
  2. {  
  3. //class defination  
  4.   
  5. }   
To demonstrate more about static classes, let us create a simple console application by creating the static  class program, as in:
  1. Open Visual Studio from "Start" -> "All programs" -> "Microsoft Visual Studio".
  2. Then go to "File" -> "New" -> "Project..." then select "Visual C#" -> "Windows" -> "Console application".
  3. Then specify the name such as Partial  class or whatever name you wish and the location of the project and click on the "OK" button. The new project is created.
  4. The Solution Explorer will look such as follows:


Now open the Program.cs file and write  the following code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace UsingStaticClass  
  7. {  
  8.     class Program  
  9.     {  
  10.         static string GetName, SetName;//only static members are allowed  
  11.   
  12.         static void Main(string[] args)  
  13.         {  
  14.             Console.WriteLine("Enter Name");  
  15.             Console.WriteLine();  
  16.             GetName = Console.ReadLine();  
  17.             SetName = Customer.GetCustDet(GetName);//The methods of static class called using the class name   
  18.             Console.Clear();  
  19.             Console.WriteLine("-------------------------");  
  20.             Console.WriteLine(SetName);  
  21.             Console.ReadLine();  
  22.         }  
  23.     }  
  24.     public static class Customer //Created using the static keyword  
  25.     {  
  26.         public static string GetCustDet(string Name) // only static methods are allowed  
  27.         {  
  28.   
  29.             return "Your Name is  " + Name;  
  30.   
  31.   
  32.         }  
  33.   
  34.     }  

Now run the application and enter an input string as in the following:
 
 
 
Now press the Enter key and the output will be as follows:
 
 
 
Hence, from the preceding all examples it's clear that static class methods are directly accessed by the static class's name itself.
Note:
  • Download the Zip file from the attachment for the full source code of the application.
Summary

In the preceding article, I have briefly explained the use of a static class to make them understandable to beginners and newcomers. If you have any suggestion regarding this article then please contact me.
 
 


Similar Articles