C
C#16mo ago
akaJB

✅ Confusion over defining a default implementation on an interface

I have an interface I'll call IWidget, and a class which implements it called Widget. I created a property IWidget with a default implementation. However, I cannot access that property on a Widget instance. My expectation was that any class/interface which implements 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? I would assume an extension for 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);
}
}
}
4 Replies
canton7
canton716mo ago
It's accessible to people who are accessing your type as an IWidget. Which is the point: you can add methods to the interface in future without breaking consumers of that interface
Moods
Moods16mo ago
If you want shared behavior among classes wouldn’t you just use an abstract class?
D.Mentia
D.Mentia16mo ago
I think default interface implementations are considered a crime, to many; they shouldn't have been added, they break the concept of what an interface is. Unless you have a very specific need (such as adding a new interface method without breaking existing implementors (as long as the calls to it are done generically via the interface)), use an abstract class implementing the interface the idea is if you know you have a Widget, you also know that Widget doesn't have that method. But if you just have an IWidget and you don't know what it is, you can use it anyway and it uses the default implementation, which is useful because you have no real way to find out what type it is or if that type actually has an implementation of that method Which is also what breaks the idea of an interface... because it's supposed to guarantee that each implementor does have that method, and now it doesn't
akaJB
akaJBOP16mo ago
Thank you for the long reply. That all makes sense and clears up my confusion.
Want results from more Discord servers?
Add your server