C# Reference ยท Classes and Object-Oriented Concepts
Default interface members in C#
Reference for C# default interface members: add methods to an interface without breaking implementers, call them through the interface, and avoid the diamond problem.
A default interface member is a method, property or indexer in an interface that has a body. Types that implement the interface get that behavior automatically and may override it. They were added in C# 8 mainly so library authors can add a member to a published interface without breaking every class that already implements it.
The default is part of the interface, not of the class. It is only visible when you call through an interface-typed variable; the implementing class does not inherit it as its own public member.
interface ILogger { void Log(string message); void Warn(string message) => Log("WARN: " + message); // default }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
interface IName { void Method() { /* default body */ } int Prop => 42; static int Counter; // static state is allowed protected void Helper() { } // protected, private, internal allowed private void Impl() { } void IOther.Method() { } // override an inherited member }
| Piece | Description |
|---|---|
| Body on a member | Makes the member optional for implementers. Without a body it stays required. |
| Access modifiers | Members are public by default; private, protected, internal, static, sealed, virtual and abstract are allowed. |
| Fields | Instance fields are not allowed; only static fields. |
Errors: CS1061 when you call a default member on the class type instead of the interface. If two interfaces derive from the same base and each overrides its default member, a class that implements both without its own version fails with CS8705 ("does not have a most specific implementation").
Usage notes
- Reach it through the interface.
obj.Method()on a class type does not find the default; cast to the interface or hold a variable of the interface type. - Not a replacement for abstract classes. There is no instance state and no constructor, so defaults are best for small convenience methods built from the abstract members (like
WarncallingLog). - Version-friendly. Add
void NewFeature() { ... }with a default and existing implementers keep compiling and working. - Diamond problem. If two interfaces override the same inherited default differently, the implementing class must state which one wins by providing its own implementation. Two unrelated interfaces that merely share a member name keep separate members.
- Structs. Calling a default member on a struct through an interface boxes the struct.
- Trait-like reuse. You can build capability mixins, but too much logic in interfaces makes behavior hard to find. Keep defaults short.
- Platform support: requires a runtime that supports it (.NET Core 3.0+, .NET 5+); not available on .NET Framework.