What is the sealed keyword in C#?
Answer
The sealed keyword prevents further inheritance or overriding. Sealed class: cannot be used as a base class — no other class can inherit from it. public sealed class SqlConnection { }. Sealed override: in a derived class, prevents further overriding of a virtual method: public override sealed void Method() { }. Use cases: (1) Preventing unintended subclassing of classes with tight invariants. (2) Performance optimization — sealed classes allow the JIT to devirtualize method calls (since the type is final, virtual dispatch is unnecessary). (3) Security — preventing malicious subclassing to override behavior. (4) Immutability design — combined with readonly fields and init-only properties. The string class in .NET is sealed (you can't derive from it). Records are sealed by default if positional. Sealing a class is generally a good practice unless you explicitly design for extensibility.
Previous
What is the difference between ref, out, and in parameters in C#?
Next
What are tuples in C#?