Static constructors in C#

Exploring the behavior of static constructors in C#

Home DailyDrop

Daily Knowledge Drop

A class can have a static constructor which is only every called once, the first time a class of the particular type is instantiated.

Today's bit of knowledge is something I know I have known at some point in the past, but due to effectively never using this feature, I find myself relearning it now - and find it interesting enough to feature on today's knowledge drop.


Characteristics

There are some characteristics of a static constructor which makes them different to a normal constructor:

  • A static constructor does not have any access modifiers or have any parameters
  • A class or struct can only have one static constructor
  • Static constructors cannot be inherited or overloaded
  • A static constructor cannot be called directly and is automatically invoked by the runtime

There are the main characteristics, however there are others - see the references link below.


Example

In the below example, we have a FileWriter class which contains two constructors (one static) and a method to write a string value to file:

public class FileWriter
{
    private static readonly string currentFileName;

    static FileWriter()
    {
        currentFileName = $"output_{DateTime.Now.ToString("yyyyMMddhhmmss")}.txt";
        
        Console.WriteLine("Static Constructor called");
        Console.WriteLine($"Setting current file to: '{currentFileName}'");
    }

    public FileWriter()
    {
        Console.WriteLine("Constructor called");
    }

    public void WriteString(string output)
    {
        Console.WriteLine($"Writing '{output}' to '{currentFileName}'");
    }
}

Instead of checking for the file existence each time the WriteString method is called, in the static constructor is leveraged to create the file once, and set the static currentFileName string.

The first time an instance of the class is instantiated, the file will be create and the currentFileName set - every other instance will now have access to the filename, and can be assured that the file exists.

We can confirm this by running the following:

var writer = new FileWriter();
var writer2 = new FileWriter();

writer.WriteString("Hello world");
writer2.WriteString("Hello universe");

The output being:

Static Constructor called
Setting current file to: 'output_20220420060039.txt'
Constructor called
Constructor called
Writing 'Hello world' to 'output_20220420060039.txt'
Writing 'Hello universe' to 'output_20220420060039.txt'

The static constructor is only called once, and both instance of the FileWriter are writing to the same file.


Notes

A very useful feature (which I am sure I will forget about again with time) when a piece of logic needs to only be executed once. The logic can be executed once to be leveraged by all instances of the class.


References

Static Constructors (C# Programming Guide)

Daily Drop 75: 17-05-2022

At the start of 2022 I set myself the goal of learning one new coding related piece of knowledge a day.
It could be anything - some.NET / C# functionality I wasn't aware of, a design practice, a cool new coding technique, or just something I find interesting. It could be something I knew at one point but had forgotten, or something completely new, which I may or may never actually use.

The Daily Drop is a record of these pieces of knowledge - writing about and summarizing them helps re-enforce the information for myself, as well as potentially helps others learn something new as well.
c# .net constructor static