0Pricing
C# Academy · Lesson

Static Constructors

Initialize static state exactly once.

Static Constructors is a free C# Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is a Static Constructor?

A static constructor initializes static data for a class. It runs automatically once, before the class is first used, and you never call it directly.

using System;

class App
{
    public static string Version;

    static App()
    {
        Version = "1.0.0";
        Console.WriteLine("Static constructor ran");
    }
}

class Program
{
    static void Main()
    {
        Console.WriteLine(App.Version);
    }
}

Syntax Rules

A static constructor uses the static keyword, has the class name, takes no parameters, and has no access modifier.

using System;

class Config
{
    public static int MaxUsers;

    static Config()
    {
        MaxUsers = 100;
    }
}

class Program
{
    static void Main()
    {
        Console.WriteLine("Max: " + Config.MaxUsers);
    }
}

Runs Exactly Once

No matter how many times you use the class, the static constructor runs only once, the first time it is needed.

using System;

class Counter
{
    public static int Instances;

    static Counter()
    {
        Console.WriteLine("Initializing static state");
        Instances = 0;
    }

    public Counter() { Instances++; }
}

class Program
{
    static void Main()
    {
        new Counter();
        new Counter();
        Console.WriteLine("Instances: " + Counter.Instances);
    }
}

When Does It Trigger?

The runtime calls it just before the first access to any static member OR the first instance creation, whichever comes first.

using System;

class Lazy
{
    public static string Data;

    static Lazy()
    {
        Console.WriteLine("Triggered now");
        Data = "ready";
    }
}

class Program
{
    static void Main()
    {
        Console.WriteLine("Before access");
        Console.WriteLine(Lazy.Data);
    }
}

Initializing Complex Static Data

Use a static constructor when static fields need real logic to set up, not just simple literals.

using System;
using System.Collections.Generic;

class Lookup
{
    public static Dictionary<int, string> Table;

    static Lookup()
    {
        Table = new Dictionary<int, string>();
        for (int i = 1; i <= 3; i++)
            Table[i] = "Item" + i;
    }
}

class Program
{
    static void Main()
    {
        Console.WriteLine(Lookup.Table[2]);
    }
}

Static Constructor vs Field Initializer

Simple values can use inline initializers. A static constructor is for setup that needs statements, loops, or error handling.

using System;

class Settings
{
    public static int Simple = 10;        // inline initializer
    public static int Computed;            // needs logic

    static Settings()
    {
        Computed = Simple * Simple;
    }
}

class Program
{
    static void Main()
    {
        Console.WriteLine(Settings.Simple + ", " + Settings.Computed);
    }
}

No Parameters Allowed

Because the runtime calls it for you, a static constructor cannot accept arguments. All its inputs must be other static data or constants.

using System;

class Clock
{
    public static int StartHour;

    static Clock()
    {
        StartHour = 9;
    }
}

class Program
{
    static void Main()
    {
        Console.WriteLine("Start: " + Clock.StartHour);
    }
}

Combining Static and Instance Constructors

A class can have both. The static one runs once for the type; the instance one runs for every object.

using System;

class Game
{
    public static string Title;
    public int Score;

    static Game()
    {
        Title = "Coddy Quest";
    }

    public Game(int score)
    {
        Score = score;
    }
}

class Program
{
    static void Main()
    {
        var g = new Game(50);
        Console.WriteLine(Game.Title + " score " + g.Score);
    }
}

Order Guarantee

The static constructor is guaranteed to finish before any instance constructor body that triggers it runs, so static state is always ready.

using System;

class Service
{
    public static string Endpoint;

    static Service()
    {
        Endpoint = "https://api.example.com";
    }

    public Service()
    {
        Console.WriteLine("Using " + Endpoint);
    }
}

class Program
{
    static void Main()
    {
        new Service();
    }
}

Use Sparingly

Static constructors add a one-time check before class use and can hurt performance if overused or if they throw. Keep them small and reliable.

using System;

class Cache
{
    public static int[] Data;

    static Cache()
    {
        Data = new int[] { 2, 4, 6, 8 };
    }
}

class Program
{
    static void Main()
    {
        Console.WriteLine(Cache.Data[3]);
    }
}

Putting It Together

Static constructors are ideal for one-time setup of shared, read-only data such as lookup tables or configuration loaded from constants.

using System;
using System.Collections.Generic;

class Currency
{
    public static Dictionary<string, string> Symbols;

    static Currency()
    {
        Symbols = new Dictionary<string, string>
        {
            ["USD"] = "$",
            ["EUR"] = "E",
            ["JPY"] = "Y"
        };
    }
}

class Program
{
    static void Main()
    {
        Console.WriteLine("USD symbol: " + Currency.Symbols["USD"]);
    }
}

Quick Check

Test your understanding of static constructors.

Recap

A static constructor uses static ClassName() with no parameters and no access modifier. It runs once, automatically, before the class is first used, to initialize static state that needs real logic. Use it sparingly for one-time setup of shared data.

using System;

class Demo
{
    public static int Value;
    static Demo() { Value = 42; }
}

class Program
{
    static void Main()
    {
        Console.WriteLine(Demo.Value);
    }
}

Frequently asked questions

Is the “Static Constructors” lesson free?

Yes — the full text of “Static Constructors” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Static Constructors”?

Initialize static state exactly once. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Static Constructors” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Defining Constructors
  2. Constructor Chaining with this
  3. Object and Collection Initializers
  4. Static Constructors
← Back to C# Academy