Static Constructors (C# Programming Guide) Home
This page is specific to:.NET Framework Version:1.12.03.03.54.0
C# Programming Guide
Static Constructors (C# Programming Guide)

A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced.

class SimpleClass
{
    // Static constructor
    static SimpleClass()
    {
        //...
    }
}

Static constructors have the following properties:

  • A static constructor does not take access modifiers or have parameters.

  • A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.

  • A static constructor cannot be called directly.

  • The user has no control on when the static constructor is executed in the program.

  • A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.

  • Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.

Example

In this example, the class Bus has a static constructor and one static member, Drive(). When Drive() is called, the static constructor is invoked to initialize the class.

public class Bus
{
    // Static constructor:
    static Bus()
    {
        System.Console.WriteLine("The static constructor invoked.");
    }

    public static void Drive()
    {
        System.Console.WriteLine("The Drive method invoked.");
    }
}

class TestBus
{
    static void Main()
    {
        Bus.Drive();
    }
}

Output

The static constructor invoked.

The Drive method invoked.

See Also

Reference

Objects, Classes and Structs (C# Programming Guide)
Destructors (C# Programming Guide)

Concepts

C# Programming Guide

Other Resources

Constructors (C# Programming Guide)

Community Content

Expanded example of Static Constructor
Added by:RidiculousX

A slightly more informative example would be:

static void Main()

{

Bus.Drive();

Bus.Drive();

}

which gives the output:

The static constructor invoked.

The Drive method invoked.

The Drive method invoked.

Thus showing that the constructor is only called once, even though 'Drive()' was called twice.

Static constructor of base class delayed
Added by:Stanley Roark
I just verified that (in .NET 2.0) base classes do not have their static constructor called until a member of the base class is used.

Instantiating the derived class will call the base class instance constructor, therefore the base class static constructor will be executed first. But static members of the derived class can be used without triggering the base class static constructor.
Static constructor of derived class is called before base class's static constructor
Added by:Prasanna J S

When base class and derived class both have static contructors, then first derived class static constructor is called and then base class static constructor is called. (In C#.NET 2.0)

This is exactly opoosite to the instance constructor hierarchy.


Can you put up a programming example for this?

A static constructor does not take access modifiers
Added by:Devashri Oza
yes i have tried it and it does not accept modifiers.

© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View