Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   How to Prevent Instantiation with an Abstract Base Class in C Sharp 4.0 (http://www.go4expert.com/forums/prevent-instantiation-abstract-base-t21332/)

arunlalds 13Mar2010 14:56

How to Prevent Instantiation with an Abstract Base Class in C Sharp 4.0
 
Mark your class as abstract.
Code:

public abstract MyClass
{
...
}
public MyDerivedClass : MyClass
{
...
}
MyClass myClass = new MyClass(); //not allowed!
MyClass myClass = new MyDerivedClass(); //this is ok

You can also mark individual methods inside a class as abstract to avoid giving them
any default implementation, as shown here:
Code:

public abstract MyClass
{
public abstract void DoSomething();
}
MyClass myClass = new MyClass();//not allowed!



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