Default Interface Methods in C# Explained
This article provides a comprehensive overview of default interface methods introduced in C# 8.0. You will learn what default interface methods are, why they were introduced to the language, how their syntax works, and the core rules governing their use in software development.
What Are Default Interface Methods?
Historically, C# interfaces were pure contracts containing only the declarations of methods, properties, events, or indexers, without any implementation details. Any class or struct that implemented an interface was required to provide the code for every declared member.
Introduced in C# 8.0, default interface methods allow developers to define concrete implementations directly within an interface. If an implementing class or struct does not provide an explicit implementation of that member, it automatically inherits the default implementation provided by the interface.
Why Were They Introduced?
The primary motivation behind default interface methods is API evolution. In earlier versions of C#, adding a new member to an already published interface was a breaking change because every external class implementing that interface would fail to compile until updated. Default interface methods solve this problem by enabling library authors to extend an existing interface with new functionality while preserving backward compatibility with older implementations.
Additionally, this feature enables C# to interoperate smoothly with APIs targeting platforms like Android (Java) and iOS (Swift), which already support default implementations in their interface protocols.
Syntax and Basic Example
A default interface method defines a body using standard C# syntax:
public interface ILogger
{
void Log(string message);
// Default implementation
void LogError(string message)
{
Log($"[ERROR] {message}");
}
}
public class ConsoleLogger : ILogger
{
public void Log(string message)
{
Console.WriteLine(message);
}
// LogError is not implemented here; the default implementation is used.
}Accessing Default Interface Methods
A key architectural rule of default interface methods in C# is that they do not become public members of the implementing class. To invoke a default implementation, the instance must be referenced through the interface type:
ConsoleLogger consoleLogger = new ConsoleLogger();
// consoleLogger.LogError("Failed"); // This will not compile.
ILogger logger = consoleLogger;
logger.LogError("Failed"); // This works correctly.If the implementing class provides its own implementation of the method, that class-specific version takes precedence, whether accessed through the class instance or the interface reference.
Key Rules and Capabilities
Default interface members introduce several expanded capabilities along with strict limitations:
- No Instance State: Interfaces still cannot maintain instance state. You cannot declare instance fields within an interface.
- Access Modifiers: Interface members can include
access modifiers such as
private,protected,internal, andvirtual. Private methods are particularly useful for sharing helper logic across multiple default methods inside the same interface. - Static Members: Interfaces can define static fields, static methods, and static properties, providing shared state or utility functionality across all consumers.
- Multiple Inheritance of Implementation: If a class implements two interfaces that provide conflicting default implementations for the same method signature, the compiler forces the class to explicitly implement that method to resolve the ambiguity.