How to Prevent Instantiation with an Abstract Base Class in C Sharp 4.0

Discussion in 'C#' started by arunlalds, Mar 13, 2010.

  1. arunlalds

    arunlalds Banned

    Joined:
    Mar 12, 2010
    Messages:
    43
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    student
    Location:
    India
    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! 
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice