Thread safety on readonly static field initialisation

后端 未结 2 1076
礼貌的吻别
礼貌的吻别 2020-12-15 05:23

If one creates a readonly static member like this:

public sealed class MyClass
{
    public readonly static MyClass Instance = new MyClass();
}
2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-15 06:01

    The class initialization is guaranteed by the specification of the C# language to be thread safe, so only one instance of MyClass will be created. You would have to ensure thread safety from that point onwards yourself. Here's an MSDN reference:

    http://msdn.microsoft.com/en-us/library/aa645612.aspx

提交回复
热议问题