Imagine these specifications are from an external dll. A class that implements an interface explicitly:
public interface IDebug
{
string GetImportantInfo();
}
public class ExternalClass : IDebug
{
public void DoSomethingImportant()
{
System.Diagnostics.Debug.WriteLine("Something important was done...");
}
string IDebug.GetImportantInfo() //Explicit implementation
{
DoSomethingImportant();
return nameof(ExternalClass);
}
}
Then this one is from internal code, where you know you need to implement the interface:
public class Debug : ExternalClass, IDebug
{
public string GetImportantInfo()
{
return nameof(Debug);
}
}
Now when I'm calling the Debug
's GetImportantInfo()
method from the subclass, the explicit implementation in the superclass is not called:
static void Main(string[] args)
{
IDebug test = new Debug();
var impInfo = test.GetImportantInfo();
System.Diagnostics.Debug.WriteLine(impInfo); //"Debug"
}
And the only slight hint I seem to get is that I don't get a compile error when adding the IDebug
interface to the Debug
class, without implementing the method:
public class Debug : ExternalClass, IDebug
{
}
Why is there no compile warning when you overwrite a superclass's implementation like this? If the base class implements it implicitly, I get a compile warning telling me to use the new
keyword. But using the new
keyword to overwrite an explicitly implemented method gives a compile warning:
The member 'Program.Debug.GetImportantInfo()' does not hide an inherited member. The new keyword is not required.
Is there an intended purpose for this, or is this a bug? If it's intended, what is the official reasoning?