From Static Constructors (C# Programming Guide)
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.
• If a static constructor throws an exception, the runtime will not
invoke it a second time, and the type will remain uninitialized for
the lifetime of the application domain in which your program is
running.
Here's a quick sample I wrote up to demonstrate what happens:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Classname.staticmethod();
Classname name = new Classname();
Console.ReadLine();
}
}
public class Classname
{
public static void staticmethod()
{
Console.WriteLine("staticmethod called");
}
static Classname()
{
Console.WriteLine("Static Constructor called");
}
public Classname()
{
Console.WriteLine("Instance Constructor called");
}
}
}
Output:
Static Constructor called
staticmethod called
Instance Constructor called