(C# Language) How and Why of static classes

A static class is a container for self-contained static functions that just operate on the input operators. The concept of a static class and static members is essentially the same as in other object oriented languages like C++ and Java. static classes provide simplicity but they are not a part of pure object oriented design. The general guidelines are to use static classes only SPARINGLY in your projects. In this tutorial we learn about the features of a static class in C#.
(Rev. 07-Nov-2022)

Categories | About |     |  

Parveen,

Table of Contents (top down ↓)

Features of a static class in C#

A static class

  1. contains only static data-members, static functions, static properties or static events.
  2. compiler adds a private instance constructor.
  3. new operator cannot be used to create objects of a static class.
  4. A static class is a sealed class - inheritance is not possible.
  5. The .NET runtime ensures that the a static class is loaded and its constructor called and its static data-members initialized before a program accesses it the first time.
  6. The static constructor of a static class is called only once.

Here's an example of a static class. The class has been marked with a static keyword. It contains a static function that returns a string.


// static class with a static function 

static class AppInfo
{
  public static string GetName()
  {
    return "My App";
  }
}

class Program
{
  static void Main()
  {
    // functions of a static class are called  
    // by using the name of the class 

    String s = AppInfo.GetName();

    Console.WriteLine(s);

  }
}

An object of a static class cannot be created. Inside main the static function of the class has been called by using the name of the class.

Video Explanation (see it happen!)

Please watch the following youtube video:

Benefits of a static class

A static class is a convenient container of static functions that are standalone, that operate only on their arguments, that do not need to touch any instance members. For example the System.IO.File is a static class of the .NET Framework. It contains a set of utility functions for file related operations. Another example of a static class is the System.Math class that contains functions for math operations on their arguments.

Static classes are now increasingly being used to hold extension methods. We'll talk of extension methods later.

The constructor of a static class is called once and it stays in memory till the lifetime of the containing app domain. This feature may be useful for some designs. So developers often mark a class as static to ensure that they do not accidentally add instance members.

There is a very marginal speed benefit to a static method. The speed benefit is in-significant, so it is hardly considered a plus point.

Problems of a static class

static classes are difficult to test.

static classes do not represent a pure object oriented design.

Experts recommend that static classes should be added to your projects only if absolutely and really necessary.

Members in a static class

As already stated, a static class can contain members of static type only. Let's write a typical static class.

A static class is marked with the static keyword, and it can contain static data members.

A static constructor is always public and used to initialize other static members of this class.

A static class can contain static properties.


// a static class can contain 
// various items of static type 

static class AppInfo
{
  // data member 
  public static String OS = "Win10";

  public static DateTime _now;

  // constructor is public by default 
  static AppInfo()
  {
    // initialize static members 
    _now = DateTime.Now;
  }

  // static property 
  public static int Version { get => 2; }

  // static function/methods 
  public static string GetName()
  {
    // static functions can call 
    // other static functions 
    Console.WriteLine("GetName . . .");

    return "My App";
  }

  // can contain static events also 
  public static event OnChange? VChange;

}

A static class can contain static functions.

It can contain static events also.


This Blog Post/Article "(C# Language) How and Why of static classes" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.