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"?
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.
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.
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.
Yep, use an interface.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With