How to Resolve Error "CS0144: Cannot create an instance of the abstract class or interface" in C#
The Compiler Error CS0144 is a fundamental Object-Oriented Programming error. The message reads: "Cannot create an instance of the abstract class or interface 'TypeName'".
In C#, you use the new keyword to create an instance (an object) of a class. However, Abstract Classes and Interfaces are incomplete definitions. They represent concepts or contracts, not concrete objects. Because they are incomplete (they may lack implementation logic), the runtime cannot create an object directly from them.
This guide explains why you cannot instantiate these types and how to correctly create objects that utilize them.
Understanding Abstract Types
To understand CS0144, think of the difference between a Concept and a Thing.
- Concrete Class (
Car): A fully defined blueprint. You can build a car. - Abstract Class (
Vehicle): A partial blueprint. It defines that vehicles move, but doesn't say how (wheels? wings?). You cannot build a generic "Vehicle," only specific types of it. - Interface (
IDrivable): A list of requirements. You cannot build a "Drivable," you can only build an object that is drivable.
The compiler throws CS0144 when you try to build the concept (new Vehicle()) instead of the concrete thing (new Car()).
Scenario 1: Attempting to Instantiate an Interface
This is a very common error. You define an interface to enforce a contract, but then try to create an instance of the interface itself.
Example of error
public interface ILogger
{
void Log(string message);
}
public class Program
{
static void Main()
{
// ⛔️ Error CS0144: Cannot create an instance of the abstract class or interface 'ILogger'
// An interface has no code implementation. The runtime wouldn't know what to do.
ILogger myLogger = new ILogger();
myLogger.Log("Test");
}
}
Solution: Implement the Interface
You must create a concrete Class that implements the interface, and instantiate that class.
public interface ILogger
{
void Log(string message);
}
// 1. Create a concrete class implementing the interface
public class ConsoleLogger : ILogger
{
public void Log(string message)
{
Console.WriteLine($"Log: {message}");
}
}
public class Program
{
static void Main()
{
// 2. Instantiate the CLASS, but you can store it in an Interface variable
// ✅ Correct: We create a 'ConsoleLogger', which satisfies 'ILogger'.
ILogger myLogger = new ConsoleLogger();
myLogger.Log("Test");
}
}
Output:
Log: Test
Scenario 2: Attempting to Instantiate an Abstract Class
Abstract classes are designed to be inherited from. They often contain abstract methods that have no body. Instantiating them directly is forbidden because calling one of those missing methods would crash the program.
Example of error
public abstract class Shape
{
// This method has no body!
public abstract double GetArea();
}
public class Program
{
static void Main()
{
// ⛔️ Error CS0144: Cannot create an instance of the abstract class 'Shape'
// If this were allowed, calling s.GetArea() would be impossible.
Shape s = new Shape();
}
}
Solution: Inherit and Override
Create a child class that provides the missing logic.
public abstract class Shape
{
public abstract double GetArea();
}
// 1. Inherit from the abstract class
public class Circle : Shape
{
public double Radius { get; set; }
// 2. Provide the implementation
public override double GetArea()
{
return Math.PI * Radius * Radius;
}
}
public class Program
{
static void Main()
{
// 3. Instantiate the Child Class
// ✅ Correct: Create a Circle, treat it as a Shape
Shape s = new Circle { Radius = 5 };
Console.WriteLine($"Area: {s.GetArea()}");
}
}
Solution: Dependency Injection (Modern Context)
In modern C# (ASP.NET Core, MAUI, etc.), you rarely instantiate services with new manually. If you see this error while trying to set up a service, you likely need to register the implementation with your Dependency Injection (DI) container.
Instead of this:
// ⛔️ Error CS0144
IMyService service = new IMyService();
Do this (in Program.cs / Startup.cs):
// Tell the app: "Whenever someone asks for IMyService, give them MyConcreteService."
builder.Services.AddTransient<IMyService, MyConcreteService>();
And inject it:
public class MyController
{
private readonly IMyService _service;
// The container injects the concrete instance here automatically
public MyController(IMyService service)
{
_service = service;
}
}
Conclusion
CS0144 ensures you don't try to use an incomplete building block as a finished product.
- Check the Type: Is it an
interfaceor anabstract class? - Find the Implementation: Look for a class that inherits from it (e.g.,
ConsoleLoggerimplementsILogger). - Instantiate the Concrete: Use
new ConcreteClass(). You can still assign it to a variable of the abstract type (AbstractType x = new ConcreteType()).