相关疑难解决方法(0)

单身人士由Jon Skeet澄清

public sealed class Singleton
{
    Singleton() {}

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

    class Nested
    {
        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static Nested() {}
        internal static readonly Singleton instance = new Singleton();
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望在我目前的C#应用​​程序中实现Jon Skeet的Singleton模式.

我对代码有两个疑问

  1. 如何访问嵌套类中的外部类?我的意思是

    internal static readonly Singleton instance = new Singleton();
    
    Run Code Online (Sandbox Code Playgroud)

    有什么叫封闭吗?

  2. 我无法理解这个评论

    // Explicit static constructor to tell C# compiler
    // not to mark type as beforefieldinit …
    Run Code Online (Sandbox Code Playgroud)

.net c# architecture singleton design-patterns

212
推荐指数
2
解决办法
4万
查看次数

标签 统计

.net ×1

architecture ×1

c# ×1

design-patterns ×1

singleton ×1