© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
5 replies
akaJB

✅ Confusion over defining a default implementation on an interface

I have an interface I'll call
IWidget
IWidget
, and a class which implements it called
Widget
Widget
. I created a property
IWidget
IWidget
with a default implementation. However, I cannot access that property on a
Widget
Widget
instance. My expectation was that any class/interface which implements
IWidget
IWidget
would get its default implementation.

When looking up some of the docs online I see this is expected behavior and has something to do with preventing breaking changes. What I'm not finding is answers to two questions:
- What's the benefit of providing an implementation to begin with if it's not direclty accessable?
- What's the best pattern to follow if there's shared behavior across all implementations of
IWidget
IWidget
? I would assume an extension for
IWidget
IWidget
but would like to confirm

Fuller example:
using System;

namespace Test
{
    public interface IWidget
    {
        public string GetName { get; }
        
        public int GetNumber => 1;    
    }
    
    public class Widget : IWidget
    {
        public string GetName => "Widget";
    }

    public static class Program
    {
        public static void Main(string[] args)
        {
            Widget widget = new Widget();
            IWidget iwidget = widget;

            // both work
            Console.WriteLine(widget.GetName);
            Console.WriteLine(iwidget.GetName);
            
            // widget has an error
            // iwidget works
            Console.WriteLine(widget.GetNumber);
            Console.WriteLine(iwidget.GetNumber);
        }
    }
}
using System;

namespace Test
{
    public interface IWidget
    {
        public string GetName { get; }
        
        public int GetNumber => 1;    
    }
    
    public class Widget : IWidget
    {
        public string GetName => "Widget";
    }

    public static class Program
    {
        public static void Main(string[] args)
        {
            Widget widget = new Widget();
            IWidget iwidget = widget;

            // both work
            Console.WriteLine(widget.GetName);
            Console.WriteLine(iwidget.GetName);
            
            // widget has an error
            // iwidget works
            Console.WriteLine(widget.GetNumber);
            Console.WriteLine(iwidget.GetNumber);
        }
    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Using a default implementation on an interface
C#CC# / help
2y ago
❔ Default implementation of a `static abstract` base interface member in derived interface
C#CC# / help
3y ago
✅ Explicit interface implementation
C#CC# / help
17mo ago
❔ Target runtime doesn't support default interface implementation.
C#CC# / help
4y ago