Skip to content

Bits of .NET

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

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
C#

C#: Use Default Interface Methods for API Evolution

- 09.07.26 - ErcanOPAK

πŸ”Œ Default Interface Methods = API Evolution

Interfaces break existing code. Default interface methods add methods without breaking. Evolve APIs safely.

πŸ“ Default Interface Basics

// Interface with default method
public interface ILogger
{
    void Log(string message);
    
    // Default implementation
    void LogError(string message)
    {
        Log($"ERROR: {message}");
    }
    
    // Default implementation
    void LogWarning(string message)
    {
        Log($"WARNING: {message}");
    }
}

// Implementation
public class ConsoleLogger : ILogger
{
    public void Log(string message)
    {
        Console.WriteLine(message);
    }
}

// Usage - doesn't need to implement LogError
var logger = new ConsoleLogger();
logger.Log("Info");
logger.LogError("Something went wrong"); // Works!

// Override default
public class DatabaseLogger : ILogger
{
    public void Log(string message)
    {
        // Write to database
    }
    
    public void LogError(string message)
    {
        // Custom error logging
        Log($"CUSTOM ERROR: {message}");
    }
}

🎯 Advanced Usage

// Static methods in interfaces
public interface IMath
{
    static int Add(int a, int b) => a + b;
}

// Private methods in interfaces
public interface ICalculator
{
    int Calculate(int a, int b);
    
    private int Multiply(int a, int b) => a * b;
    
    int Power(int a, int b)
    {
        int result = 1;
        for (int i = 0; i < b; i++)
        {
            result = Multiply(result, a);
        }
        return result;
    }
}

// Multiple inheritance
public interface ILoggable
{
    void Log(string message);
    
    void LogInfo(string message) => Log($"INFO: {message}");
}

public interface IAuditable
{
    void Audit(string action);
    
    void AuditCreate(string entity) => Audit($"CREATE: {entity}");
}

public class UserService : ILoggable, IAuditable
{
    public void Log(string message) => Console.WriteLine(message);
    public void Audit(string action) => Console.WriteLine(action);
    
    // Both interfaces work
}

// Diamond problem - explicit implementation
public interface IA
{
    void Method() => Console.WriteLine("A");
}

public interface IB
{
    void Method() => Console.WriteLine("B");
}

public class C : IA, IB
{
    public void Method()
    {
        // Must be implemented
        Console.WriteLine("C");
    }
}

πŸ’‘ Best Practices

  • Use for API evolution
  • Keep defaults simple
  • Document default behavior
  • Use sparingly (avoid complexity)
  • Consider interface segregation

“Default interface methods enable safe API evolution. Add methods without breaking. Essential for library design.”

β€” Library Architect

Related posts:

.NET Core JSON Serializer Pitfalls β€” Why Your Properties Don’t Serialize

.NET Core: Fix Memory Leaks by Understanding IDisposable and Using Patterns

C# Task.WhenAll Can Kill Your Server (Concurrency Trap)

Post Views: 4

Post navigation

SQL: Use JSON Functions for Modern Data
C#: Use File Scoped Namespaces for Cleaner Code

Leave a Reply Cancel reply

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

July 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  
« Jun    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (960)
  • How to add default value for Entity Framework migrations for DateTime and Bool (898)
  • How to make theater mode the default for Youtube (857)
  • Get the First and Last Word from a String or Sentence in SQL (840)
  • How to select distinct rows in a datatable in C# (814)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (597)
  • Add Constraint to SQL Table to ensure email contains @ (582)
  • Average of all values in a column that are not zero in SQL (545)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (512)
  • Find numbers with more than two decimal places in SQL (460)

Recent Posts

  • C#: Use Using Statements for Resource Management
  • C#: Use Lambda Expressions for Concise Code
  • SQL: Use GROUP BY for Data Aggregation
  • .NET Core: Master Routing for Clean URLs
  • Git: Use Reset to Undo Local Changes
  • Ajax: Use Axios for HTTP Requests
  • JavaScript: Understand Hoisting
  • HTML: Use Web Storage for Client-Side Data
  • CSS: Use Filter Effects for Visual Magic
  • Windows 11: Unlock God Mode for All Settings

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (960)
  • How to add default value for Entity Framework migrations for DateTime and Bool (898)
  • How to make theater mode the default for Youtube (857)
  • Get the First and Last Word from a String or Sentence in SQL (840)
  • How to select distinct rows in a datatable in C# (814)

Recent Posts

  • C#: Use Using Statements for Resource Management
  • C#: Use Lambda Expressions for Concise Code
  • SQL: Use GROUP BY for Data Aggregation
  • .NET Core: Master Routing for Clean URLs
  • Git: Use Reset to Undo Local Changes

Social

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