C# Interface Implementation: Syntax and Patterns
Learn how to implement interfaces in C# with practical examples: implicit vs explicit implementations, default methods, interface inheritance, and dependency injection.

In C#, implementing an interface means providing concrete members that satisfy the contract the interface declares. The most common approach is implicit implementation, where you define a public member with the same signature as the interface member. For example:
public interface ILogger
{
void Log(string message);
}
public class ConsoleLogger : ILogger
{
public void Log(string message)
{
Console.WriteLine(message);
}
}Here, ConsoleLogger implements ILogger by providing a public Log method. This is the default style and works well when the class has no other member with the same name.
Implicit vs Explicit Interface Implementation
C# also supports explicit implementation, where you prefix the member name with the interface name. This is useful when you want to hide the member from the class's public API or when two interfaces declare members with identical signatures.
public interface IFileWriter
{
void Write(string content);
}
public interface IDatabaseWriter
{
void Write(string content);
}
public class Writer : IFileWriter, IDatabaseWriter
{
void IFileWriter.Write(string content)
{
// Write to file
}
void IDatabaseWriter.Write(string content)
{
// Write to database
}
}In this example, both interfaces declare a Write method. Explicit implementation lets you provide separate logic for each. The methods are only accessible when the object is referenced through the respective interface type. Calling writer.Write(...) on a Writer instance will not compile, because the explicit implementations are not part of the class's public contract.
The choice between implicit and explicit implementation depends on how you intend the class to be used. Implicit implementation is simpler and makes the member available directly on the class. Explicit implementation is useful when you need to disambiguate or when you want to prevent direct calls.
Handling Member Name Conflicts
When a class implements multiple interfaces that share member names, you have two options: provide a single implicit implementation that satisfies both, or use explicit implementations to give each interface its own behavior. The first approach works when the behavior is identical; the second is required when the behavior differs.
Consider a scenario where two interfaces both have a Connect method, but one is meant for a network connection and the other for a database connection. An implicit implementation would force you to choose one behavior, which may be incorrect for one of the interfaces. Explicit implementation avoids this by binding each method to its interface.
A less obvious conflict occurs when an interface member has the same name and signature as an existing public method on the class. The existing method can satisfy the interface implicitly, but it now serves both direct class callers and callers using the interface. If you later change that public method, both audiences are affected. An explicit implementation keeps the interface method separate from the class's public method.
Default Interface Methods in C# 8+
Starting with C# 8, interfaces can include default implementations for their members. This lets you add members to an interface without requiring every existing implementer to provide them. For example:
public interface IReport
{
string Generate();
string GenerateSummary()
{
var full = Generate();
return full.Length <= 100 ? full : full.Substring(0, 100);
}
}Classes that implement IReport need to provide Generate. They can define their own GenerateSummary if they want a custom summary, but if they do not, the default implementation is used when the member is accessed through IReport.
Default interface methods have a subtle behavior: they are not automatically available on the implementing class unless the class is accessed through the interface. If you have a class Report : IReport that does not define GenerateSummary, calling report.GenerateSummary() on a Report instance will not compile. You must cast to IReport first. This is because the default method belongs to the interface, not the class.
Interface Inheritance and Composition
Interfaces can inherit from other interfaces, allowing you to build a hierarchy of contracts. For example:
public interface IRepository
{
void Add(object entity);
}
public interface IReadOnlyRepository
{
object GetById(int id);
}
public interface IReadWriteRepository : IRepository, IReadOnlyRepository
{
}In this example, a class that implements IReadWriteRepository must provide all members declared by both IRepository and IReadOnlyRepository. This approach lets you define focused contracts and combine them as needed.
When designing interfaces, avoid creating large interfaces that force implementers to provide unrelated members. Such an interface is hard to implement correctly. Split responsibilities into smaller interfaces and let classes implement only what they need.
Using Interfaces with Dependency Injection
Interfaces are central to dependency injection because they allow you to substitute implementations without changing the consuming code. For instance, a service class can depend on an IEmailSender rather than a concrete SmtpEmailSender. This makes the service testable and flexible.
public interface IEmailSender
{
void Send(string to, string subject, string body);
}
public class NotificationService
{
private readonly IEmailSender _emailSender;
public NotificationService(IEmailSender emailSender)
{
_emailSender = emailSender;
}
public void Notify(string userEmail)
{
_emailSender.Send(userEmail, "Welcome", "Hello!");
}
}The implementation of IEmailSender can be registered in the dependency injection container and swapped for a mock during unit tests. This pattern is a direct benefit of interface implementation, and it is one of the main reasons interfaces are used in modern C# applications.
Performance and Design Considerations
Interface calls go through interface dispatch, so they can have a small overhead compared with direct method calls. In most applications this overhead is negligible. If you are writing a hot path, measure before deciding that an interface is the problem. An abstract class can share implementation, but it restricts implementers to a single base class.
Another design consideration is the interface segregation principle. A class should not be forced to implement members it does not use. When you design an interface, keep it small and focused. If you find that an interface has many members, consider splitting it into multiple interfaces. This makes implementation easier and reduces the chance of throwing NotImplementedException for unused members.
Finally, interfaces cannot contain instance fields or store per-instance state. They are best understood as contracts for the members a class can provide, and C# 8+ lets them include default implementations. Shared logic can be placed in default implementations, abstract classes, or extension methods.
Try matching browser tools
All toolsSmall utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related C# guides
All C# guidesC# Dispose vs Destructor: Key Differences Explained
c# dispose vs destructor: Understand the difference between Dispose and destructors in C#, when each runs, and how to manage unmanaged resources correctly.
C# Variable Declaration: Syntax and Practical Usage
Learn how to declare variables in C# with explicit types, var, and nullable annotations, and understand how defaults, scope, and readability affect your choices.
C# Primary Constructors for Classes: Syntax and Usage
Learn how to use C# primary constructors for classes: syntax, initialization, dependency injection, and limitations with practical examples.
C# IComparer: Custom Sorting with IComparer<T>
Learn how to implement IComparer<T> in C# to define custom sort order for arrays and lists, handle nulls, and avoid common performance pitfalls.
More in C#
All C# guidesThe guides filed next to this one in C#, in library order.