To prevent a class from being inherited, the sealed keyword in C# can be used. The NotInheritable keyword can be used in VB.NET to prevent accidental inheritance of the class.
In C# (and .NET in general), you can prevent a class from being inherited (i.e., making it “sealed” or “final”) by using the sealed
keyword in the class declaration. This indicates that the class cannot be subclassed further. Here’s an example:
public sealed class MyClass
{
// Class members and methods
}
By marking the class as sealed
, you ensure that it cannot be used as a base class for any other class. This is useful when you want to prevent inheritance for reasons such as maintaining control over the class implementation or ensuring that certain behaviors are not overridden.