3

C# has a useful explicit interface feature that lets you create methods that implement interfaces while avoiding potential name conflicts.

public abstract class BaseClass : IDisposable {
  public int Value;
  void IDisposable.Dispose() => Value = 1;
}

You can even override these methods in subclasses, so long as the subclass also explicitly lists that it implements the interface.

public class SubClass : BaseClass, IDisposable {
  void IDisposable.Dispose() => Value = 2;
}

static void Main() {
  BaseClass obj = new SubClass();
  ((IDisposable)obj).Dispose();
  Console.WriteLine(obj.Value); // 2
}

From within a subclass, you can usually call base.Whatever to access the baseclass versions of methods. But with explicit interface implementations, this syntax isn't valid. Also, there's no way to cast your base to the interface in order to call the method.

How do I access the logic within my base class's explicit interface implementations?

benjamin.popp
  • 547
  • 1
  • 6
  • 20
  • 4
    You can't access access an overridden method without the sub class allowing you to. Maybe through some crazy reflection. The bigger question is why you'd want to. It's basically breaking inheritance. – Yuriy Faktorovich May 25 '17 at 14:33
  • There https://stackoverflow.com/questions/7090122/explicit-interface-implementation-cannot-be-virtual You are also given some workarounds. – Wiktor Zychla May 25 '17 at 14:38
  • Yuriy, I'm currently using explicit interfaces to allow strong return types in subclasses. I haven't hit a point where I need to call into a base class's implementation. But I wanted to know if it was possible. – benjamin.popp May 25 '17 at 14:48
  • Explicit interface implementation is not "useful", it is a necessary evil. If a class implements ICowboy and IPainter then its Draw() method cannot reasonably both shoot a gun and apply paint. You have to make one of the interfaces a victim and implement its Draw() method with an explicit interface implementation. So only use it when you have to. And when you did then you must cast `this` to the (interface) so you can call the correct method. – Hans Passant May 25 '17 at 15:26

1 Answers1

0

I hope this be useful:

 public abstract class BaseClass : IDisposable
 {
        public int Value;
        void IDisposable.Dispose() => DoSomething();
        public void DoSomething() => Value = 1;
 }

 public class SubClass : BaseClass, IDisposable
 {
        void IDisposable.Dispose()
        {
            if (Condition())
                DoSomething();
            else
                DoSomethingElse();
        }

        void DoSomethingElse() => Value = 2;

        private bool Condition()
        {
            return true;
        }


 }
Saeid
  • 13,224
  • 32
  • 107
  • 173
  • What I'm getting from this is that the base class's explicit implementation basically has to just forward to another method of at least protected visibility. But there's still no way to actually call the base method, you just have to call to the forwarding method. Still, it's better than nothing. – benjamin.popp May 25 '17 at 15:13
  • @benjamin.popp There's no way to call the base method, it is against the inheritance. – Saeid May 25 '17 at 15:30