I am trying to create a c# class, but I dont want it to be inherited. How can I accomplish that?
In C# you can use the sealed keyword in order to prevent a class from being inherited. Also in VB.NET you can use the NotInheritable keyword to prevent accidental inheritance of the class.
In C++ 11, we can make the base class non-inheritable by using the final specifier. For eg, the following code gives a compile error as the base class is declared as final.
There are 2 ways to stop or prevent inheritance in Java programming. By using final keyword with a class or by using a private constructor in a class.
sealed
is the word you're looking for, and a link for reference
public sealed class MyClass { }
And then just create your class as normal, however you won't be able to inherit from it.
You can however still inherit from a different class like so
public sealed class MyClass : MyBaseClass { }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With