Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   Prevent Inheritance (http://www.go4expert.com/forums/prevent-inheritance-t21344/)

arunlalds 13Mar2010 15:28

Prevent Inheritance
 
Mark the class as sealed.
Code:

sealed class MyClass
{
...
}

Structs are inherently sealed
Prevent Overriding of a Single Method
Put sealed as part of the method or property definition.
Code:

class ParentClass
{
public virtual void MyFunc() { }
}
class ChildClass : ParentClass
{
//seal base class function into this class
public sealed override void MyFunc() { }
}
class GrandChildClass : ChildClass
{
//yields compile error
public override void MyFunc() { }
}



All times are GMT +5.5. The time now is 21:16.