'密封类中的受保护成员'警告(单例类)

6 c# wpf singleton sealed

我已经实现了一个单例类,并且不断收到警告,我正在编写的方法是一个'在密封类中声明的新受保护成员'.它不会影响构建,但我真的不想忽略警告,以防以后出现问题?我理解一个密封类是一个不能被继承的类 - 所以它的方法不能被覆盖,但我仍然不明白为什么下面的代码会给我警告(是因为使用了单例设计?):

namespace WPFSurfaceApp
{
public sealed class PresentationManager
{
    PresentationManager()
    {
    }

    protected void MethodName()
    {
    }

    public static PresentationManager 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 PresentationManager instance = new PresentationManager();
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:警告是关于MethodName()方法.编辑:将public void MethodName()更改为protected void MethodName()

Jam*_*are 16

警告是因为protected在无法继承的类中没有意义.它在逻辑上与private一个sealed类完全相同.

这是不是一个错误,本身,但是编译器试图提请您注意的事实,使得它protected,而不是private将提供你没有好处,不得做你打算什么(如果你想要它是一个子可见类,不能存在于密封类中).

所以,是的,你可以放心地忽略它,但是protected在一个sealed类中拥有成员在逻辑上是不一致的.

编译器警告CS0628的 MSDN条目