When To Use Static Classes In C#

The static modifier in C# declares a static member of a class. The static modifier can be used with classes, properties, methods, fields, operators, events, and constructors, but it cannot be used with indexers, finalizers, or types other than classes.

C# supports static classes and static members. A static class can’t be instantiated. A C# class can have static or non-static members. A static member has only one copy of the member, regardless of the number of instances. Static members and their values belong to the type itself, rather than the object. If multiple instances of a class are created, the last updated value of a static member will be available to all instances.
 
The static modifier in C# declares a static member of a class. The static modifier can be used with classes, properties, methods, fields, operators, events, and constructors, but it cannot be used with indexers, finalizers, or types other than classes.
 
The static keyword in C# language is used to declare a static class. Learn more about static keyword here Static Keyword In C#
 

Static Class

 
A static class cannot be instantiated. All members of a static class are static and are accessed via the class name directly, without creating an instance of the class.
 
The following code is an example of a static class, CSharpCorner. We know that all members of the class are static. 
  1. public static class CSharpCorner  
  2. {  
  3. // Static fields  
  4. public static string Name = "C# Corner";  
  5. public static string Founder = "Mahesh Chand";  
  6. public static DateTime YearFounded = new DateTime(2000, 01, 01);  
  7. public static string Location = "Downingtown, PA";  
  8. public static string Description =  
  9. "Online community of software and data developers";  
  10. public static int GetAgeOfWebsite()  
  11. {  
  12. return 1;  
  13. }  
  14. }  
Static classes have the following characteristics: 
  • Static classes cannot contain Instance Constructors.
  • Static classes contain only static members.
  • Static classes cannot be instantiated.
  • Static classes are sealed. That means, you cannot inherit other classes from instance classes. 
You can learn more about static classes here: Static Class in C#
 

Static Members

 
A static or non-static class static constructors, properties, methods, fields, operators, and events. Static properties and static methods are the most-used static members.
 

Static Constructor

 
Static constructor can't be parameterized.
Static constructor doesn't have any access modifier because it doesn't have message passing and is used during domain processing.
Static Constructor is used to initialize static data members of the class.
 

Static Field

 
A field with the static keyword represents a static field. Static fields can be declared as follows by using the static keyword. 
  1. public static class HistoryTeacher  
  2. {  
  3. // static field  
  4. public static string Subject = "History";  
  5. }  
The following code calls a static field. 
  1. Console.WriteLine(HistoryTeacher.Subject);  
When we declare static data members inside a class, it can be initialized with a value as shown above. All un-initialized static fields automatically get initialized to their default values when the class is loaded for the first time.
 

Static Property

 
Static properties are used to get or set the value of static data members of a class. The following code example declares four static properties. 
  1. public static class HistoryTeacher  
  2. {  
  3. // private fields  
  4. private static string name;  
  5. private static string school;  
  6. private static int rank;  
  7. private static int years;  
  8. // static properties  
  9. public static int Years { get => years; set => years = value; }  
  10. public static int Rank { get => rank; set => rank = value; }  
  11. public static string School { get => school; set => school = value; }  
  12. public static string Name { get => name; set => name = value; }  
  13. }  
The following code example sets static property values. 
  1. HistoryTeacher.Name = "Mahesh Chand";  
  2. HistoryTeacher.Rank = 2;  
  3. HistoryTeacher.School = "Garnet Valley High School";  
  4. HistoryTeacher.Years = 5;  
In the same way, you can access a static property by using the class name. 
  1. Console.WriteLine(HistoryTeacher.Name);  

Static Method

 
Static methods are shared methods. They can be called with the class name and static method name only. You cannot instantiate a static method.
 
Static methods only use static data members to perform calculation or processing.
 
The following code example declares a static method that takes two int values as method arguments. 
  1. public static class HistoryTeacher  
  2. {  
  3. // static method  
  4. public static int CalculateScore(int rank, int years)  
  5. {  
  6. return rank * years;  
  7. }  
  8. }  
The following code example calls a static method. 
  1. Console.WriteLine(HistoryTeacher.CalculateScore(3, 5));  

Why use static classes and static members?

 
Static classes are used as containers for static members. Static methods and static properties are the most-used members of a static class. All static members are called directly using the class name. Static methods do a specific job and are called directly using a type name, rather than the instance of a type.
 
Here is a list of few use cases of static classes. 
  • A Math class with all static methods. Static classes are useful and provide an easy way to access its members that does not need to work differently for different objects. 
  • Above listed CSharpCorner class. We know the value of CSharpCorner class members such as its founder, launch date, location, and description, will never change regardless of its objects. 
  • App Configuration class that has all static settings about an app and the values of settings don’t change based on the objects or users.
  • A DatabaseConfig class that may have members such as database name, server name, port number, and even a connection string. We know that these values will not change for objects. 
Static classes and static members are useful because they do not require instances created for each new object. That means, they consume fewer resources and no duplication of the same class or member is needed in memory.
 
Static members make code cleaner.
 
In theory, a static should give better performance compare to an instance. However, it is unnoticeable.
 

Complete Code Example

 
Here is a complete program that shows how to use static class and static members. 
  1. using System;  
  2. namespace StaticInCSharp  
  3. {  
  4. class Program  
  5. {  
  6. static void Main(string[] args)  
  7. {  
  8. Console.WriteLine(HistoryTeacher.Subject);  
  9. HistoryTeacher.Name = "Mahesh Chand";  
  10. HistoryTeacher.Rank = 2;  
  11. HistoryTeacher.School = "Garnet Valley High School";  
  12. HistoryTeacher.Years = 5;  
  13. Console.WriteLine(HistoryTeacher.Name);  
  14. Console.WriteLine(HistoryTeacher.CalculateScore(3, 5));  
  15. Console.ReadKey();  
  16. }  
  17. }  
  18. public static class HistoryTeacher  
  19. {  
  20. // static field  
  21. public static string Subject = "History";  
  22. // private fields  
  23. private static string name;  
  24. private static string school;  
  25. private static int rank;  
  26. private static int years;  
  27. // static properties  
  28. public static int Years { get => years; set => years = value; }  
  29. public static int Rank { get => rank; set => rank = value; }  
  30. public static string School { get => school; set => school = value; }  
  31. public static string Name { get => name; set => name = value; }  
  32. // static method  
  33. public static int CalculateScore(int rank, int years)  
  34. {  
  35. return rank * years;  
  36. }  
  37. }  
  38. }  

Summary

 
In this article, I discussed what static classes are in C# and how to use a static class in your C# application.