Skip to main content

Cannot create an instance of interface

In This Article

CodeRush Classic shows the Cannot create an instance of interface code issue if an instance creation statement references an interface.

#Fix

Create an instance of a class implementing the current interface.

#Purpose

Highlights the instance creation statements, which would cause the Cannot create an instance of the abstract class or interface compilation error.

#Example

interface IMyClass
{
    void OutputText(string text);
}
public class TestClass
{
    public TestClass()
    {
        IMyClass objVar = new IMyClass();
    }
}

Fix:

interface IMyClass
{
    void OutputText(string text);
}
class MyClass: IMyClass
{
    public override void OutputText(string text)
    {
        Console.Write(text);
    }
}
public class TestClass
{
    public TestClass()
    {
        IMyClass objVar = new MyClass();
    }
}