Bob.at.Indigo.Health
Bob.at.Indigo.Health

Reputation: 11895

Can't get C# default interface method to compile

C# 8.0 has a new feature that lets you add a default implementation to a method on an interface. Either I'm doing something wrong or this feature doesn't work as advertised. (I'm guessing it's the former.)

I created a new .NET Core 3.1 console app with the following code:

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var xxx = new MyClass { MyInt = 5 };
            Console.WriteLine(xxx.GetItNow());
        }
    }

    public interface ITest
    {
        int MyInt { get; set; }
        int GetItNow() => MyInt * 2;
    }

    public class MyClass : ITest
    {
        public int MyInt { get; set; }
    }
}

The Console.WriteLine(xxx.GetItNow())); statement doesn't compile because

Myclass does not contain a definition for 'GetItNow()'...

So, the compiler is happy with the fact that MyClass doesn't explicitly reference GetItNow() (it doesn't complain the MyClass doesn't implement the interface). But it doesn't see the default interface member as a public method of the class that implements the interface.

Am I missing something, or is something broken?

Upvotes: 4

Views: 1024

Answers (3)

Henry
Henry

Reputation: 496

If you want to avoid having to cast or redeclare the variable when you use the method, you can always use and extension method which references the default implementation

public interface ITest
{
    int MyInt { get; set; }
    int GetItNow() => MyInt * 2;
}

public static class ITestExtensions
{
    public static int GetItNow(this ITest test) => test.GetItNow();
}

public class MyClass : ITest
{
    public int MyInt { get; set; }
}

Then, you can call myTest.GetItNow() as you wanted to in your question instead of ((ITest) myTest).GetItNow().

Upvotes: 0

Jorge
Jorge

Reputation: 67

https://stackoverflow.com/a/61717913/779967 is excellent and clear answer, I was trying to do the same with this new feature but got stopped by the compiler. Now I know why.

If your intention is to mark some class with an interface and get some functionality attached to it. I’ve been using extension methods on the interface itself to achieve this effect. The only downside is that only functions are supported at this time not properties.

public interface ITest
{
    int MyInt { get; set; }
}

public static class ITestExtensions
{
    public static int GetItNow(this ITest self)
    {
        return self.MyInt * 2;
    }
}

Upvotes: -1

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

Well, interface default method belongs to the interface not to a class which implements it; so you have two possibilities:

Cast:

  Console.WriteLine(((ITest)xxx).GetItNow()));

Declaration change (preferable; MyClass is implementation detail, often it's a dependency; ITest - contract is the only thing which matter):

  ITest xxx = new MyClass { MyInt = 5 };
  // xxx is ITest, so xxx.GetItNow() is legal now 
  Console.WriteLine(xxx.GetItNow());

Upvotes: 9

Related Questions