I've implemented a singleton class and keep getting the warning that a method I'm writing is a 'new protected member declared in a seal class.' It's not affecting the build but I don't really want to ignore the warning in case it causes problems later on? I understand a sealed class is a class that cannot be inherited - so it's methods cannot be overridden, but I still don't get why the following code would give me the warning (is it due to the use of the singleton design?):
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();
}
}
EDIT: The warning is regarding MethodName() method. EDIT: Change public void MethodName() to protected void MethodName()
The warning is because
protected
does not make sense in a class that can't be inherited from. It will logically be exactly the same asprivate
for asealed
class.It's not an error, per se, but the compiler is trying to draw your attention to the fact that making it
protected
instead ofprivate
will provide you no benefit and may not be doing what you intended (if you intended it to be visible to a sub-class, which can't exist in a sealed class).So, yes, you can safely ignore it, but it's logically inconsistent to have
protected
members in asealed
class.MSDN Entry for Compiler Warning CS0628