Skip to main content

How to Implement an Interface in C#

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

An interface says what a class can do, without saying how. In C#, any class that implements it has to provide every member it lists, and code can then use all those classes through the interface, without knowing which one it has.

Syntax​

Syntax
interface <IName>
{
<ReturnType> <MethodName>(<parameters>);
}

class <ClassName> : <IName>
{
public <ReturnType> <MethodName>(<parameters>) => <expression>;
}
PartWhat it does
interface <IName>Declares the interface. Its members have no body, only the signature. Names start with I by convention
class <ClassName> : <IName>The class promises to implement the interface
public <ReturnType> <MethodName>(...)The class's version of each member, with the same signature, and public

Microsoft's interfaces guide: "An interface defines a contract: a group of related methods, properties, events, and indexers that a class or struct must implement." And: "A class or struct lists the interfaces it implements after a colon in its declaration."

The Example​

An IGreeter interface has one method, Greet. An EnglishGreeter and a GermanGreeter implement it, each in its own language. The program puts one of each in an array of IGreeter and greets Ana with both.

Prerequisites​

  • The .NET 10 SDK or newer, which runs a single .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which puts an EnglishGreeter and a GermanGreeter in an array of IGreeter and prints greeter.Greet of Ana for each, with an IGreeter interface declaring string Greet(string name), and two classes that implement it and return Hello and Hallo with the name.

main.cs
IGreeter[] greeters = [new EnglishGreeter(), new GermanGreeter()];

foreach (IGreeter greeter in greeters)
{
Console.WriteLine(greeter.Greet("Ana"));
}

interface IGreeter
{
string Greet(string name);
}

class EnglishGreeter : IGreeter
{
public string Greet(string name) => $"Hello, {name}!";
}

class GermanGreeter : IGreeter
{
public string Greet(string name) => $"Hallo, {name}!";
}

Step 2: Run It​

Terminal
dotnet run main.cs
Output
Hello, Ana!
Hallo, Ana!

A zsh terminal where dotnet run main.cs prints Hello, Ana! and Hallo, Ana!.

  1. The array's type is IGreeter[], yet it holds an EnglishGreeter and a GermanGreeter: both are IGreeters.
  2. The loop called Greet the same way each time, and each object answered with its own version.

The command is the same in zsh, bash and PowerShell.

Every Member Must Be There​

A class that names the interface but leaves out a member doesn't compile:

main.cs
Console.WriteLine("Start");

interface IGreeter
{
string Greet(string name);
}

class GermanGreeter : IGreeter
{
}
Output
/Users/you/demo/main.cs(8,23): error CS0535: 'GermanGreeter' does not implement interface member 'IGreeter.Greet(string)'

The build failed. Fix the build errors and run again.

The guide: "The class must provide an implementation for every member declared in the interface". The error names the class and the member it's missing.

An Interface Can't Be Created​

An interface is only the promise, so there's nothing to create:

main.cs
IGreeter greeter = new IGreeter();

interface IGreeter
{
string Greet(string name);
}
Output
/Users/you/demo/main.cs(1,20): error CS0144: Cannot create an instance of the abstract type or interface 'IGreeter'

The build failed. Fix the build errors and run again.

Create a class that implements it, and store it in a variable of the interface type: IGreeter greeter = new EnglishGreeter();.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out a member the interface listserror CS0535: 'GermanGreeter' does not implement interface member 'IGreeter.Greet(string)'Add the member, with the same signature
new IGreeter()error CS0144: Cannot create an instance of the abstract type or interface 'IGreeter'Create a class that implements it
Giving the member another signature, such as Greet() without the nameCS0535 again: it doesn't count as the interface's Greet(string)Copy the signature exactly

Troubleshooting​

SymptomLikely causeFix
CS0535 on a classA member of the interface is missing or differsImplement it as the error names it
CS0144 on a newnew was used on the interfacenew a class instead

FAQ​

Why use the interface type for the variable?​

So the code works with any class that implements it. The loop above never needed to know which greeter it had, and a third greeter would work without changing the loop.

Can a class implement more than one interface?​

Yes, separated by commas after the colon. The class then has to provide every member of every interface it lists.

Conclusion​

Declare what a class must do with interface IName { ... }, implement it with class Name : IName, and provide every member, public and with the same signature. Then use the classes through the interface type. A missing member is CS0535, and new on an interface is CS0144.

Additional Resources​