换句话说,这个单例实现是线程安全的:

public class Singleton
{
    private static Singleton instance;

    private Singleton() { }

    static Singleton()
    {
        instance = new Singleton();
    }

    public static Singleton Instance
    {
        get { return instance; }
    }
}

当前回答

静态构造函数将在允许任何线程访问该类之前完成运行。

    private class InitializerTest
    {
        static private int _x;
        static public string Status()
        {
            return "_x = " + _x;
        }
        static InitializerTest()
        {
            System.Diagnostics.Debug.WriteLine("InitializerTest() starting.");
            _x = 1;
            Thread.Sleep(3000);
            _x = 2;
            System.Diagnostics.Debug.WriteLine("InitializerTest() finished.");
        }
    }

    private void ClassInitializerInThread()
    {
        System.Diagnostics.Debug.WriteLine(Thread.CurrentThread.GetHashCode() + ": ClassInitializerInThread() starting.");
        string status = InitializerTest.Status();
        System.Diagnostics.Debug.WriteLine(Thread.CurrentThread.GetHashCode() + ": ClassInitializerInThread() status = " + status);
    }

    private void classInitializerButton_Click(object sender, EventArgs e)
    {
        new Thread(ClassInitializerInThread).Start();
        new Thread(ClassInitializerInThread).Start();
        new Thread(ClassInitializerInThread).Start();
    }

上面的代码产生下面的结果。

10: ClassInitializerInThread() starting.
11: ClassInitializerInThread() starting.
12: ClassInitializerInThread() starting.
InitializerTest() starting.
InitializerTest() finished.
11: ClassInitializerInThread() status = _x = 2
The thread 0x2650 has exited with code 0 (0x0).
10: ClassInitializerInThread() status = _x = 2
The thread 0x1f50 has exited with code 0 (0x0).
12: ClassInitializerInThread() status = _x = 2
The thread 0x73c has exited with code 0 (0x0).

尽管静态构造函数运行了很长时间,但其他线程会停止并等待。所有线程都读取静态构造函数底部的_x集合的值。

其他回答

静态构造函数保证是线程安全的。 另外,看看DeveloperZen上关于Singleton的讨论: http://web.archive.org/web/20160404231134/http://www.developerzen.com/2007/07/15/whats-wrong-with-this-code-1-discussion/

虽然其他答案大多是正确的,但对于静态构造函数还有另一个警告。

根据ECMA-335通用语言的第二节10.5.3.3竞赛和死锁 基础设施

类型初始化本身不会产生死锁,除非有一些代码 从类型初始化器(直接或间接)显式调用 调用阻塞操作。

下面的代码导致死锁

using System.Threading;
class MyClass
{
    static void Main() { /* Won’t run... the static constructor deadlocks */  }

    static MyClass()
    {
        Thread thread = new Thread(arg => { });
        thread.Start();
        thread.Join();
    }
}

原作者是Igor Ostrovsky,见他的帖子在这里。

使用静态构造函数实际上是线程安全的。静态构造函数保证只执行一次。

来自c#语言规范:

类的静态构造函数在给定的应用程序域中最多执行一次。静态构造函数的执行由应用程序域中发生的以下事件中的第一个触发: 创建类的实例。 类的任何静态成员都被引用。

所以,是的,你可以相信你的单例会被正确地实例化。

Zooba提出了一个很好的观点(比我早15秒!),即静态构造函数不能保证线程安全的对单例对象的共享访问。这需要用另一种方式来处理。

静态构造函数被锁定。当类型初始化式正在运行时,任何其他试图以触发类型初始化式的方式访问该类的线程都将阻塞。

但是,运行类型初始化式的线程可以访问未初始化的静态成员。因此,如果从UI线程运行类型初始化器,请确保不要从类型初始化器调用Monitor.Enter() (lock(){})或ManualResetEventSlim.Wait()——这些是“可中断”的等待,会导致事件循环运行,在类型初始化器尚未完成时执行程序的任意其他部分。

最好使用托管块而不是非托管块。WaitHandle。WaitOne WaitHandle。WaitAny WaitHandle。WaitAll、监控。输入、监控。TryEnter,线程。加入,GC。WaitForPendingFinalizers等等都对Thread有响应。中断和线程中止。同样,如果你的线程是在一个单线程的公寓,所有这些托管阻塞操作将正确泵消息在你的公寓当你的线程被阻塞:

虽然所有这些答案都给出了相同的一般答案,但有一个警告。

记住,泛型类的所有潜在派生都被编译为单独的类型。因此,在为泛型类型实现静态构造函数时要谨慎。

class MyObject<T>
{
    static MyObject() 
    {
       //this code will get executed for each T.
    }
}

编辑:

下面是演示:

static void Main(string[] args)
{
    var obj = new Foo<object>();
    var obj2 = new Foo<string>();
}

public class Foo<T>
{
    static Foo()
    {
         System.Diagnostics.Debug.WriteLine(String.Format("Hit {0}", typeof(T).ToString()));        
    }
}

在控制台:

Hit System.Object
Hit System.String