Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why explicit interface implementation works that way? [duplicate]

Tags:

c#

.net

interface

Why does this program print "Child Name" instead of "Base Name"?

using System;

class Program
{
    static void Main(string[] args)
    {
        var child = new Child();
        Console.WriteLine(((INamedObject)child).Name);
        Console.ReadLine();
    }
}

interface INamedObject
{
    string Name { get; }
}

class Base : INamedObject
{
    string INamedObject.Name
    {
       get
       {
         return "Base Name";
       }
    }
}

class Child : Base, INamedObject
{
   public string Name
   {
      get
      {
         return "Child Name";
      }
   }
}

I expect that when I cast child to INamedObject, Name property of INamedObject explicit implementation will be invoked. But what happens is that Child.Name property is called.

Why when I declare Child class like that (removing INamedObject from Child):

class Child : Base
{
   public string Name
   {
      get
      {
         return "Child Name";
      }
   }
}

it starts printing "Base Name"?

like image 706
ekalchev Avatar asked Nov 28 '19 16:11

ekalchev


People also ask

Can an interface implement methods C#?

With C# 8.0, you can now have default implementations of methods in an interface. Interface members can be private, protected, and static as well. Protected members of an interface cannot be accessed in the class that extends the interface. Rather, they can be accessed only in the derived interface.

What is implicit interface in C#?

An implicit interface implementation is where you have a method with the same signature of the interface. An explicit interface implementation is where you explicitly declare which interface the method belongs to.

What is an implicit interface?

Implicit Interface ImplementationsInterfaces are implemented implicit by declaring a public member in the class with the same signature of the method as defined in the interface and the same return type. This is how you normally implement interfaces.

What do you use if you want to ensure that all methods and properties are implemented?

Yep, use an interface.


1 Answers

The other answers do not correctly identify the C# feature that you have stumbled upon.

You have discovered a somewhat confusing feature of C# called "interface re-implementation". The rule is that when a derived class specifically re-states an interface that is already implemented by the base class, then the compiler starts over and re-does the interface mapping from scratch.

If that happens then methods of more derived types are given precedence over methods of less derived types because we assume that the developer who is developing the more derived type has a better implementation than the developer who developed the base class version. After all, if the derived version was worse, the developer would not have implemented it!

This rule allows you to decide whether you want a derived class to replace a base class interface mapping or not, because sometimes you want to, and sometimes you don't.

See my 2011 article about this feature for more details:

https://blogs.msdn.microsoft.com/ericlippert/2011/12/08/so-many-interfaces-part-two/

You might also find this answer helpful:

Abstract base classes that implement an interface

For the section of the specification that describes this language feature, see

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/interfaces#interface-re-implementation

like image 50
Eric Lippert Avatar answered Oct 09 '22 15:10

Eric Lippert