Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Default Interface Methods in C#
Default interface methods are a game-changing feature introduced in C# 8.0 that allow developers to add new methods to an interface without breaking existing implementations. This feature enables interface evolution while maintaining backward compatibility with existing code.
Syntax
Following is the syntax for defining a default interface method
public interface IInterfaceName {
void MethodName() {
// default implementation
}
}
A class can use the default implementation or override it
public class ClassName : IInterfaceName {
// Uses default implementation automatically
// OR override with custom implementation:
public void MethodName() {
// custom implementation
}
}
Traditional Interface Methods in C#
Traditionally, interfaces in C# could only contain declarations of methods, properties, events, or indexers, but not their implementations. Any class or struct that implemented the interface had to provide the implementation for each member of the interface.
using System;
// Traditional interface - only declarations
public interface ITraditional {
void DoSomething(); // Must be implemented by all classes
}
public class OldWay : ITraditional {
public void DoSomething() {
Console.WriteLine("Must implement this method");
}
}
public class Program {
public static void Main() {
OldWay obj = new OldWay();
obj.DoSomething();
}
}
The output of the above code is
Must implement this method
Using Default Interface Methods
Default interface methods provide a default implementation directly in the interface. Classes implementing the interface can use this default implementation without providing their own.
Example
using System;
public interface IGreetable {
void Greet(string name) {
Console.WriteLine($"Hello, {name}!");
}
}
public class User : IGreetable {
// No need to implement Greet method, uses default implementation
}
public class Program {
public static void Main() {
IGreetable user = new User();
user.Greet("John");
}
}
The output of the above code is
Hello, John!
Overriding Default Interface Methods
Even though an interface provides a default implementation, implementing classes can still provide their own implementation to override the default behavior.
Example
using System;
public interface IGreetable {
void Greet(string name) {
Console.WriteLine($"Hello, {name}!");
}
void Welcome() {
Console.WriteLine("Welcome to our application!");
}
}
public class User : IGreetable {
// Uses default implementations for both methods
}
public class Admin : IGreetable {
public void Greet(string name) {
Console.WriteLine($"Hello, {name}. You have admin privileges.");
}
// Uses default Welcome() method
}
public class Program {
public static void Main() {
IGreetable user = new User();
IGreetable admin = new Admin();
user.Greet("Alice");
user.Welcome();
admin.Greet("Bob");
admin.Welcome();
}
}
The output of the above code is
Hello, Alice! Welcome to our application! Hello, Bob. You have admin privileges. Welcome to our application!
Interface Evolution with Default Methods
Default interface methods solve the problem of interface evolution. You can add new methods to existing interfaces without breaking implementations.
Example
using System;
public interface ICalculator {
int Add(int a, int b);
// New method added with default implementation
int Multiply(int a, int b) {
Console.WriteLine("Using default multiplication");
return a * b;
}
}
public class BasicCalculator : ICalculator {
public int Add(int a, int b) {
return a + b;
}
// No need to implement Multiply - uses default
}
public class AdvancedCalculator : ICalculator {
public int Add(int a, int b) {
return a + b;
}
public int Multiply(int a, int b) {
Console.WriteLine("Using optimized multiplication");
return a * b;
}
}
public class Program {
public static void Main() {
ICalculator basic = new BasicCalculator();
ICalculator advanced = new AdvancedCalculator();
Console.WriteLine("Basic: " + basic.Multiply(4, 5));
Console.WriteLine("Advanced: " + advanced.Multiply(4, 5));
}
}
The output of the above code is
Using default multiplication Basic: 20 Using optimized multiplication Advanced: 20
Key Rules
-
Default interface methods require C# 8.0 or later and .NET Core 3.0+ or .NET 5+.
-
Access default methods through interface reference, not class reference directly.
-
Default methods can call other interface methods and access static members.
-
Classes can choose to override default implementations or use them as-is.
Conclusion
Default interface methods in C# 8.0 enable interface evolution without breaking existing implementations. They provide default behavior that classes can inherit or override, making interfaces more flexible and maintainable while preserving backward compatibility.
