Protected member warning in a private class (singleton class)

I have implemented a singleton class and continue to receive a warning that the method I am writing is "a new protected member declared in the print class". This does not affect the assembly, but I really do not want to ignore the warning if it causes problems later? I understand that a closed class is a class that cannot be inherited, so the methods cannot be overridden, but I still don’t understand why the following code will give me a warning (is this related to using 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: Warning refers to MethodName (). EDIT: change public void MethodName () to protected void MethodName ()

+6
source share
4 answers

The warning is that protected does not make sense in a class that cannot be inherited. It will logically be exactly the same as private for the sealed class.

This is not a mistake in itself, but the compiler is trying to draw your attention to the fact that creating protected instead of private will not do you any good and may not do what you intended (if you were meant to be visible to subclass that cannot exist in a private class).

So, yes, you can safely ignore it, but it is logically incompatible to have protected members in the sealed class.

MSDN Record for Compiler Warning CS0628

+14
source

This is obvious because it makes no sense. What will the protected member use if the class can not is inherited

How MSDN Says

Types declare protected members so that inherited types can access or override an element. By definition, you cannot inherit from a sealed type, which means that protected methods on private types cannot be called.

+4
source

Think about it when you review the code yourself. You see something that makes no sense as far as you can see. There are several possibilities:

  • The developer did something stupid.
  • The developer has done something too smart to be obvious to you.
  • The developer did something sensible that no longer makes sense due to changes that have occurred on average.
  • The developer has done what still does not make sense, but will happen if a planned change occurs.

In the first case, they must fix it.

In the second case, they must document it.

In the third case, they must change it; this will make a difference, but the code will make more sense and may have little benefit.

In the fourth case, they must document it and either make this change or return from it sooner rather than later.

In any case, you would like to discuss this with them.

The same here, it makes no sense to add a protected element to a private class. I have no idea why you did this, and cannot decide which of the four cases above, but one of them. A warning emphasizes this. Depending on which of the four actions acts, depending on which of the four cases acts.

+2
source

I say that you are playing with C #. Seriously!!
The static class says that we cannot declare protected members because static classes cannot be created. In fact, when we write protected members in static classes, it will raise an error during assembly.
In a closed class, it throws a warning at build time. I think that as static classes, private classes should also give an ERROR, not a WARNING. If this difference should be there, then why?

0
source

Source: https://habr.com/ru/post/905723/


All Articles