The article focuses on the fact that in the sealed class the Dispose method must be private , otherwise protected virtual . I quote:

This method should be protected virtual for non- sealed classes and private for sealed classes.

What is the benefit of such use?

In essence, a protected virtual will still not be able to be overridden for the sealed class, since heirs cannot be created. But if suddenly we want to remove sealed from the class in which we use, as advised, private , then we will have to redo the protected virtual . Why not leave in all cases protected virtual ?

  • 3
    The presence of a virtual in a sealed class looks out of place - there should not be any explicit virtual methods there. "Suddenly" sealed not removed, sealed is an architectural solution. - ixSci

2 answers 2

I think the problem is in Russian. :) Unlike English in Russian there are not so many shades of responsibility as in English. In most cases, the word "must" is used both as "must" and as "it is better to do so."

Just rewrite for yourself this phrase from the article as :)

This method should be defined as protected virtual for non-sealed classes and private for sealed classes.

The fact is that if a sealed class is derived from another base class, then it can override the protected virtual functions of the base class. It cannot itself declare virtual functions, since it does not make sense, although it can declare protected members, which also does not make sense. In the latter case, the compiler will issue a warning.

  • Microsoft in FxCop rules requires defining a method as virtual if it implements the interface, or define the whole class as sealed. Perhaps this is a kind of reference to this rule. - Uranus
  • @Uranus Yes, if the class can be inherited, then this method should be overridden. For sealed classes, it makes no sense to declare a method as protected. - Vlad from Moscow
  • After replacing "must" by "should" the essence of the question does not clear up. Pay attention to the last paragraph in the question. - αλεχολυτ
  • @alexolut The compiler does not allow declaring virtual protected functions in a sealed class. This is logical, since the sealed class is not inherited. However, if a sealed class inherits another class, then it can override the virtual functions,, of the base class. It is also permitted to declare other members of the class protected, although this also does not make sense for a sealed class. - Vlad from Moscow
  • @VladfromMoscow if you reformulate the answer, focusing on the impossibility to compile (compile error) sealed class with a virtual (not override ) function, then I think I can accept it. The “must / should” translation situation does not reflect the real essence. - αλεχολυτ

Actually, the answer is already there, but it is somewhat veiled, and the author of the answer did not deign to bring it to a more obvious state. Therefore, I will answer myself.

sealed class cannot contain definitions of new virtual functions . When you try to do this, a compilation error CS0549 will be displayed:

 error CS0549: New virtual member 'member-name' is declared in a sealed class 'class-name' 

So to answer the question it was enough to compile an elementary example and try to compile it. A misunderstanding of the obvious arose from the fact that and have a slightly different meaning in the virtual . Those. in c ++, in a derived class for an overdetermined virtual function, you can write virtual , virtual + override , override , or you can add nothing at all - the function will still remain virtual and overdetermined (with matching signatures). In c #, for overridden virtual functions, override should be used exclusively.

Although the reason for using private instead of protected override is still not completely clear. But in this form of the question has not yet sounded.