🔧 C#: Fix an Interface Default Method Hiding an Explicit Override
C# lets a class override an interface’s default method using explicit interface implementation syntax – writing void IFoo.Method() { … } instead of a plain public method – which is useful when the class already has an unrelated member with the same name, or when the override shouldn’t appear on the class’s own public surface at all. But explicit interface implementation has always worked this way for ordinary interface members too: the member it creates is never visible through the class’s own type, only through a reference or cast to the interface itself. Overriding a default method this way means calling it through the concrete class type doesn’t fall back to the interface’s default behavior instead – it fails to compile entirely, because the class’s own public surface never had that method on it in the first place.
🔎 The Problem
interface IFormatter
{
string Format(object value) => value?.ToString() ?? "";
}
class JsonFormatter : IFormatter
{
// explicit interface implementation - NOT part of JsonFormatter's
// own public surface, even though it overrides the default body
string IFormatter.Format(object value) =>
System.Text.Json.JsonSerializer.Serialize(value);
}
var formatter = new JsonFormatter();
formatter.Format(42);
// CS1061: 'JsonFormatter' does not contain a definition for 'Format'
IFormatter asInterface = formatter;
asInterface.Format(42); // works fine - calls the override correctly
✅ The Fix
- Call the overriding method through an interface-typed reference or an explicit cast, such as ((IFormatter)formatter).Format(42), wherever the concrete class type doesn’t expose it, instead of assuming any override you can see in the source is reachable from the class type directly.
- If the method is meant to be part of the class’s normal public API, implement it as a plain public method matching the interface signature instead of using explicit interface implementation syntax – a plain implementation is accessible from both the class type and the interface type.
- Document the choice clearly whenever a team deliberately uses explicit interface implementation to override a default method (to avoid a naming collision, or to keep it out of IntelliSense on the class type), since the resulting ‘missing method’ compile error at ordinary class-typed call sites is easy for someone unfamiliar with the pattern to misread as a broken interface implementation.
🚨 Why This Is Dangerous
A correct, intentional explicit override failing to compile at every normal class-typed call site can read like the interface implementation itself is broken, tempting someone to ‘fix’ it by deleting the explicit override entirely – which does make the code compile again, but only by falling back to the interface’s original default behavior everywhere, silently losing whatever behavior the override was there to provide.
Explicit interface implementation was never about overriding a default method quietly – it’s about keeping that member off the class’s own surface entirely, default method or not.



