The static qualifier is available at both the class level as well as the individual member level (both fields and methods) in C#.  This article discusses when to use it a the class level versus when it makes sense to use it at the member level.
 
‘Static’ qualifier at a class level
When you want to ensure that your class only contains static members – and no accidental instance fields/members. The compiler will help ensure this if the class is marked as static. An example would be a Utility classs where every user of the class will need to use exactly the same ‘instance’ and the  same methods in the instance (note that a static class is never strictly ‘instantiated’ – it doesn’t live on the normal heap but rather in a special heap called the Loader Heap. This makes sense because anything on the heap is going to get garbage collected – and you would not want your static class to be garbage collected.). Static classes are less common than instance classes with a few static members (described below). Before we get to those, here are some examples of when you would need to use ‘static’ at a class level:
A utility class – Typically – you will not need any instance methods inside your utility class. That would defeat the purpose of providing the same utility methods to all consumers of the utility methods in the class.
A struct converter class (or any converter in general) – Suppose your application has a variety of data structs – your own defined user-types which are used throughout your application. You may need to convert back and forth between different data types. A simple example would be if you had a Dollar class and a Euro class and needed to go back and forth between these. You could define a static CurrencyConverter class as shown below:
So  – at the class level, Utility classes, Conversion classes etc. are ideally suited for the static qualifier.
Code Snippet
  1. /// <summary>
  2. ///  Utility class used for doing conversions – ideal candidate for 'static' (one instance only) class
  3. /// </summary>
  4. static class CurrencyConverter
  5. {
  6.     static Euro ConvertToEuro(Dollar inDollar)
  7.     {
  8.       // do conversion…
  9.        return euro;
  10.     }
  11.  
  12.     static Dollar ConvertToDollar(Euro inEuro)
  13.     {
  14.         // do conversion…
  15.         return dollar;
  16.     }
  17. }

 
Static Members (Instance classes with static members)
This is the more commonly used form of the ‘static’ qualifier. Members of instance classes often need to marked ‘static’. The basic criteria is: is there any member variable that is common across all instances of this class? For e.g. – in an Employee class – even though each employee instance is unique – they all belong to the same ‘Employer’ – hence a field called EmployerName (or EmployerID) inside an Employee class would be a good candidate for being marked ‘static’.
Code Snippet
  1. class Employee
  2.  {
  3.      // common across all instances
  4.      static string _employerName = "Microsoft";
  5.  
  6.  
  7.      // unique to each instance
  8.      int _employeeId;
  9.      string _employeeName;
  10.  }

Another example may be if you are trying to model cars. The number of ‘wheels’ on each of your car instances would always be 4.
Code Snippet
  1. class Car
  2.     {
  3.         // 'static' makes this common across all instances
  4.         static string _numWheels = 4;
  5.  
  6.  
  7.         // unique to each instance
  8.         int _milesPerGallon;
  9.         string _make;
  10.     }

In summary, while C# offers the ‘static’ qualifier at both the class level as well as individual member level, it is rare to use it at the class level – except for utility classes. The real advantage of static at the class level is having the compiler ensure for you that there are no instance level members in your class. In practice, one finds a lot more use of regular, non-static classes – which have the occasional static members to deal with commonality across all instances.

Anuj holds professional certifications in Google Cloud, AWS as well as certifications in Docker and App Performance Tools such as New Relic. He specializes in Cloud Security, Data Encryption and Container Technologies.

Initial Consultation

Anuj Varma – who has written posts on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.