Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
C#

C#: Fix an Interface Default Method Hiding an Explicit Override

- 26.11.26 - ErcanOPAK

🔧 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.

— C# Best Practice

Related posts:

C#: Fix a Regex Marked RegexOptions.Compiled That Silently Makes Startup Slower Without Making Match...

C#: Fix a Regex Marked RegexOptions.Compiled That Silently Makes Startup Slower Without Making Match...

C#: Fix an IEnumerable Being Enumerated Twice Because It Was Never Materialized With ToList Before B...

C#: Fix an IEnumerable Being Enumerated Twice Because It Was Never Materialized With ToList Before B...

async void — The Exception Black Hole

async void — The Exception Black Hole

Views: 6
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Photoshop: Fix a Batch Action Skipping Non-Latin Filenames
Windows 11: Fix File Explorer Search Ignoring Indexed Results

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,080 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 999 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 998 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 874 views
  5. 05How to select distinct rows in a datatable in C# 869 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 673 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 632 views
  8. 08Average of all values in a column that are not zero in SQL 586 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 563 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com