What good is a C#?

Asked

Viewed 2,296 times

14

I saw a class in C# that was stated like this, in a reply I read in Soen:

public sealed class Link
{
     // Resto do código
}

What is the key word for sealed in the above case?

5 answers

16


Well, in short the sealed is to indicate that the class cannot be inherited by others. It is the same as the final java.

Many say it should be the standard, since inheritance is almost never the solution. Inheritance brings a very strong baggage and a burden to maintenance since any change in this class will potentially affect the functioning of all derivatives.

A data structure as a class is useful, but not so much inheritance. Of course, it has its uses. Organizing everything together in the object is much more advantageous and helps much more than allowing direct reuse. In most cases the person wants to make an inheritance should make a composition.

One struct is implicitly sealed, and should have standardized this in the class. It should have an optional word like open, inheritable, or even a virtual to use something already existing, or something similar to this, what matters is the idea. The fundamental difference of a class is that it is always by reference, and unless optimized, has the storage location in the heap (some today admit that it might be better not to be that standard and the programmer have to say where it goes, but it’s a small current for C#).

Sealed classes are simple and reliable, require fewer tests and can evolve better. Of course, if done well. Classes open to inheritance are problematic even when everything is done right. It gets worse when the class can be inherited and instantiated. Some consider that classes open to inheritance should always be abstract, which I increasingly agree with. Many OOP problems would not exist like this. But I also admit that there is case that can be useful to have both, done very carefully.

Then use sealed by default until you are sure you need the inheritance. Then draw the class for the inheritance, which is absurdly more difficult.

In C# the idea of the default inheritance was copied from Java and was a bad idea. It existed before, but made more sense in other languages. Java, by its philosophy, was wrong and C# was in the wave.

In methods

This attribute is also used in methods, much rarer. In this case you are prohibiting a method being superimposed when it was originally virtual, and porting polymorphic. It only makes sense to use it in methods that are virtually inherited and that want to prevent it from remaining virtual from there. So it only makes sense to exist in an inherited class and in a method that in the ascendant was marked as virtual. Methods not explicitly virtual can be considered sealed implicitly.

Again I consider it a mistake. If you want to keep being virtual you should have to say it. Although in this case there are its disadvantages. I prefer the implicit for coherence, but being explicit has its merit.

The name is weird, but that’s what it had. It would be a nonvirtual.

  • Just out of curiosity, the VB.NET uses the name NotInheritable which I find more didactic. The idea of inheritance by default I think has more to do with other OO languages and not with Java. Although there is a quote from the creator of C# who claims that C# did not copy Java, it seems to me that the idea of sealed has the same compiler optimization reasons.

  • @Pagotti I don’t even like the term OO, because in practice this is something very secondary in them. But it’s okay to use. Indeed, but what language are we talking about then? I love this idea of not copying Java, although it is a little bit too much, and I gave a lecture these days on this, I like the idea of having copied more until C++, but in the background much was copied from Java yes, including this error. C++ had no idea that inheritance was not possible. If it was a perfected copy of C++ sealed would be default. Since it is not a copy of the error committed in Java. I did not understand the compiler optimization

  • The optimization part is because of a phrase in the C#language specification document: "The Sealed Modifier is primarily used to Prevent Unintended Derivation, but it also Enables Certain run-time optimizations. In particular, because a Sealed class is known to Never have any derived classes, it is possible to Transform virtual Function Member invocations on Sealed class instances into non-virtual invocations"

  • There is discussion in the OS about this (I researched it now) and there are controversies. It must be something from the first versions of the language. Maybe this doesn’t apply anymore in current versions.

  • in fact the text is not very good, because the fact of the class is sealed does not mean much unless no method can be virtual, but optimization will occur qq way if the method is not virtual, in any kind of class. Show the discussion.

  • The text is from Microsoft’s own manual. One of the discussions is here: https://stackoverflow.com/q/2134/1700943

  • @Pagotti and the text is bad. The answer accepted in the discussion is bad , so much so that it does not have so many votes, a good thing of Soen is that even if there also have of the "maria vai com as others" who vote in the accept, has much + q vote on other things. Patrick Smacchia’s article is good, but redundant, because there’s the point of facilitating the maintenance that I said, and there’s the performance that’s not because it’s sealed, but because there’s no virtual, the fact that it’s sealed is collateral and contextual, and the third point has to do with the first, is maintenance.

  • There’s a OOP freak who thinks open class is good, in general pragmatists have more voice and speak what I said. Clearly the guy with the accepted answer understands the effects, but not the why, there say some things that seem right, but are not in essence. The most voted answer is better. Almost always votes say more than acceptance. I think that the classification of the answers should not consider the acceptance. have others there with few votes who get lost for not being accepted (most people do not look at those who were not accepted or at least had many votes).

  • There’s even one with an update that’s great and not enough votes, even though the guy didn’t understand the collateral issue. And C# 8 should help even more with non-null reference types. And as always what’s best there is Jon Skeet’s comment further down.

Show 4 more comments

6

It is a modifier that, in classes, defines that other classes cannot inherit from the specified.

public class A { }
public sealed class B : A { } // Funciona normal. B herda de A e não pode ser herdada.
public class C : B { }        // Erro de compilação.

This modifier can also be used in class members that can be inherited. This allows the child class to use the behavior defined in the parent class, but cannot change it.

class A
{
    protected virtual void Metodo1() => Console.WriteLine("Classe A -> 1");
    protected virtual void Metodo2() => Console.WriteLine("Classe A -> 2");
}

class B : A
{
    protected sealed override void Metodo1() => Console.WriteLine("Classe B -> 1");
    protected override void Metodo2() => Console.WriteLine("Classe B -> 2");
}

class C : B
{         
    protected override void Metodo1() => Console.WriteLine("Classe C -> 1");     
    // (^) Erro de compilação.
    protected override void Metodo2() => Console.WriteLine("Classe C -> 2");
}

4

In the case of classes, the modifier sealed serves to indicate that it cannot be inherited.

class A {}      
sealed class B : A {}

That is, B may inherit from A, but if I create a C class and try to inherit from B, it will cause a build error.

// Causa erro
class C : B {}

The same thing happens with methods Overriding. If you try to override a method with the modifier sealed, will also cause a build error.

In your example, the modifier indicates that no other class can inherit from the class Link

  • 1

    Banaca, from the look of it, final of PHP/Java.

4

sealed, when used in a class declaration, prevents other classes from inheriting from it.

public class TesteA { //... }
public sealed class TesteB : TesteA { //... }

In this case above, TesteB inherits from TesteA, but an attempt to inherit from TesteB will cause a build error.

The keyword sealed can also be used in methods or properties:

public class TesteA { 
    protected virtual void Hello() { Console.WriteLine("TesteA cumprimenta você!"); }
}
public class TesteB : TesteA { 
    sealed protected override void Hello() { Console.WriteLine("TesteB cumprimenta você!"); }
}

Here will trigger CS0239 build error, cannot overwrite a member declared as sealed

public class TesteC : TesteB { 
    protected override void Hello() { Console.WriteLine("TesteC cumprimenta você!"); }
}

3

It is a modifier that prevents other classes from inheriting from this class. This modifier can also be used in properties or methods.

A basic example that exemplifies this:

class B {}
sealed class A: B {}

that heritage works where A inherits from B, but, if it is the other way around it does not work by restricting the modifier.

Reference: Sealed (Reference of C#)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.