How to Implement an Interface in C#
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
interface <IName>
{
<ReturnType> <MethodName>(<parameters>);
}
class <ClassName> : <IName>
{
public <ReturnType> <MethodName>(<parameters>) => <expression>;
}
| Part | What 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
.csfile withdotnet run. This guide uses .NET SDK 10.0.404.
Step 1: Write the Code
Create a file named main.cs in an empty folder:

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
dotnet run main.cs
Hello, Ana!
Hallo, Ana!

- The array's type is
IGreeter[], yet it holds anEnglishGreeterand aGermanGreeter: both areIGreeters. - The loop called
Greetthe 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:
Console.WriteLine("Start");
interface IGreeter
{
string Greet(string name);
}
class GermanGreeter : IGreeter
{
}
/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:
IGreeter greeter = new IGreeter();
interface IGreeter
{
string Greet(string name);
}
/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
| Mistake | What happens | Do this instead |
|---|---|---|
| Leaving out a member the interface lists | error 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 name | CS0535 again: it doesn't count as the interface's Greet(string) | Copy the signature exactly |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
CS0535 on a class | A member of the interface is missing or differs | Implement it as the error names it |
CS0144 on a new | new was used on the interface | new 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
- Interfaces, on Microsoft Learn.
- interface keyword, on Microsoft Learn.
