How to use "new" on an abstract class

How to use "new" on an abstract class

Problem Description:

I’ve tried to search for other questions like mine but I can’t find any good result
I’m trying to use a DLL that has a method that need to be called with a new class but that class is abstract.


i tried to use new IClientHandler but it just errors

Solution – 1

You’re trying to instantiate an abstract class in C# code. This is not possible, as an abstract class is a class that cannot be instantiated on its own. Instead, you would need to create a non-abstract class that extends the abstract class and then use that to create an object.

Here’s an example:

abstract class AbstractClass
    public abstract void AbstractMethod();

class ConcreteClass : AbstractClass
    public override void AbstractMethod()
        Console.WriteLine("I'm a non-abstract method!");

// Now we can create an instance of ConcreteClass
var instance = new ConcreteClass();

You can then call the AbstractMethod on the instance object, since it has been implemented in the ConcreteClass.

Also my small mistake, as @ewerspej noticed:

IClientHandler is an interface, not an abstract class. Neither can be instantiated. Interfaces require implementations that can be instantiated and abstract classes must be extended by non-abstract classes that can be instantiated.

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.