3

In C# 8.0 interfaces can have default implementations for some members. These implementations are explicit meaning that they are inaccessible through the class instance and we must cast to the interface type to call them. Is there a way to expose this implementation on the class instance with the same name? It is possible to expose it under another name by casting the this pointer to the interface type and calling the method like:

void A()
{
   ((ISomething)this).B();
}

However I can't seem to find a way to expose the implementation with the original name of B because if I declare a method B it counts as the one implementing the interface which causes infinite recursion. Is there some way to expose the implementation without copying it or there is something I am missing?

To clarify, I am looking for a way to achieve traits-like functionality i.e. being able to import the implementation from the interface directly into the class's public API without changing the method name (presumably the name in the interface was the best one). The question is not about how to call the method as a user of the class but how to make it part of the public API of the class.

Extension methods are one solution but default interface implementations have access to protected members and can be overloaded.

Stilgar
  • 22,354
  • 14
  • 64
  • 101
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/203549/discussion-on-question-by-stilgar-in-c-8-0-is-there-a-way-to-expose-default-mem). – Samuel Liew Dec 03 '19 at 19:49
  • @SamuelLiew without those comments the question would still be closed and nobody would be able to answer. It's those comments that clarified the question. Yes, they were a lot but yes, they were important – Panagiotis Kanavos Dec 04 '19 at 10:32
  • Great, please help edit the question into shape then. – Samuel Liew Dec 04 '19 at 10:49
  • @Stilgar can you add the important parts from the comments into the question? – Panagiotis Kanavos Dec 04 '19 at 15:25
  • @PanagiotisKanavos I added a clarification. Do you consider anything else from the discussion important to the question? – Stilgar Dec 04 '19 at 21:07

2 Answers2

2

There's no simple way to do it; various ugly workarounds are possible that all boil down to delegating to some other method (a static method or relying on a helper class that doesn't itself implemented the method) to access it, and even then you need to pass in state somehow. There's a proposal for a base(T) syntax, draft specification here, which should allow base(ISomething).B() to refer to the implementation without causing a cyclic reference. This was originally slated to be part of C# 8, but this proved to be too ambitious and it was cut. As of writing, it's a candidate for inclusion in C# 9.

Jeroen Mostert
  • 27,176
  • 2
  • 52
  • 85
2

One possibility is to provide properties for them. This is what I do when I need to refer to explicit implementations from within the class.

class Something : IInterfaceA, IInterfaceB {
   public IInterfaceA A => this;
   public IInterfaceB B => this;
}

...

something.A.AMethod();
something.B.BMethod();

You might also consider using extension methods instead of default implementations. They are somewhat similar anyway.

interface IInterface {
}

static class IInterfaceExtensions {
   public static void DoSomething(
      this IInterface me
   ) {
      // do something
   }
}
Dave Cousineau
  • 12,154
  • 8
  • 64
  • 80
  • An extension method has more access to the class too. The OP, in a comment now deleted, mentioned that a DIM has access to protected methods too, although that reverses the actual relation - a DIM adds a demand for those methods, it doesn't just have access to them – Panagiotis Kanavos Dec 04 '19 at 10:34