1

I try to use the new C# default interface methods. Can anyone explain the following code and why this happens?

Minimal test example:

public static void Main()
{
    Class c = new Class();
    IInterface i = new Class();
    c.M1();
    i.M1();
    (i as IInterface).M1();
    Class2 c2 = new Class2();
    Class2Base c2b = new Class2();
    c2.M2();
    c2b.M2();
    (c2 as Class2Base).M2();
}

public interface IInterface
{
    public void M1() => Console.WriteLine("Call from interface");
}

public class Class : IInterface
{
    public void M1() => Console.WriteLine("Call from class");
}

public class Class2 : Class2Base
{
    public void M2() => Console.WriteLine("Call from class2");
}

public class Class2Base
{
    public void M2() => Console.WriteLine("Call from class2base");
}

Outputs:

Call from class
Call from class
Call from class
Call from class2
Call from class2base
Call from class2base

I expect to see "Call from interface" in the second call similar to when using normal inheritence like in the second example.

To summarize:

Why is the interface method hidden and (maybe) how can i still call it.

Also, this is my first question here so any tips are welcome

9
  • 3
    The culprit is "default". Class implements M1, so it is called. If you make another class that "implements IInterface" but in which you do not implement M1, then the default from the interface is called. It has only marginally to do with "classic" inheritance.
    – Fildor
    Apr 21, 2020 at 15:53
  • Just as a side note, albeit for Java, still oop, --> stackoverflow.com/questions/36335838/… Apr 21, 2020 at 16:04
  • A way to wrap your head around why this is different is to explore why this feature was added. Without it, you cannot add methods to your interface, without breaking your existing code. If you don't have that feature and add a function, you need to touch ALL implementations. Depending on how that implementation was done, this can range from easy-peasy to "just no". Having it, you can at least add the new function with a "dummy" non-breaking implementation and have all new implementations support a new feature. Neat, innit?
    – Fildor
    Apr 21, 2020 at 16:07
  • 2
    I note that you should have gotten a warning on class2 noting that there is a possible unintentional shadowing there. If you intend there to be two non-virtual methods with the same name, the convention is to tell the compiler that this is not a mistake by decorating the shadowing method with new. If you intend the shadowing method to be a virtual override, you need to mark the original method as virtual and the new method as override. Apr 21, 2020 at 16:08
  • 1
    I understand though why this is a little confusing. Methods which implement interface members behave in some ways as though they are virtual, and in fact if you look at the generated code, they are virtual, but for the purposes of the C# language's rules, they are not treated as virtual, and that can be a little confusing. Apr 21, 2020 at 16:11

1 Answer 1

3

Why is the interface method hidden?

Default interface methods are just that. The method in the interface is the default implementation if no other implementation is provided.

In every one of your cases, you provided an implementation, so the default will not be used.

When you provide a non-default implementation, we assume that the new implementation is better than the default implementation; if it was not better, you would not have provided it, right? You would not have done work for no reason, and presumably you want your work to be used. It would be foolish for the runtime to call the default implementation when a better implementation exists.

How can I still call it?

In your code, you don't.

If you want to always have a default implementation that you can call, you can do something like:

public interface IInterface
{
    public void M1() => MyHelperMethods.M1(this);
}

public static class MyHelperMethods
{
    public static void M1(IInterface i) => 
      Console.WriteLine("Call from static");
}

And now if you want to call the default implementation you can call MyHelperMethods.M1 directly.

1
  • I recently reread your blog post on FAIC where you describe why implementing an interface that is less accessible than a public class is ok, but the same does not hold true for base classes. Does this feature change your assessment on that? Apr 30, 2020 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.