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

public class Singleton
{
    private static Singleton instance;

    private Singleton() { }

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

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

当前回答

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

其他回答

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

    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集合的值。

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

根据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,见他的帖子在这里。

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

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

来自c#语言规范:

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

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

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

只是为了卖弄学问,但没有静态构造函数,而是静态类型初始化器,这里有一个循环静态构造函数依赖的小演示,它说明了这一点。