Making a class not inherited

Viewed 12865

I am trying to create a c# class, but I dont want it to be inherited. How can I accomplish that?

2 Answers

Adding to the PostMan's answer, we can achieve the same by making the Class Constructor Private.

class NotInheritable
    {

        private NotInheritable()
        {
            //making the constructor private
        }
    }

 class Derived : NotInheritable { }

Now the class NotInheritable will not be inheritable, as the compiler will prompt the error:

NotInheritable.NotInheritable() is inaccessible due to its protection level.

Related