C#: why Base class is allowed to implement an interface contract without inheriting from it?
Posted
by etarassov
on Stack Overflow
See other posts from Stack Overflow
or by etarassov
Published on 2010-05-31T10:00:21Z
Indexed on
2010/05/31
10:02 UTC
Read the original article
Hit count: 232
I've stumbled upon this "feature" of C# - the base class that implements interface methods does not have to derive from it.
Example:
public interface IContract
{
void Func();
}
// Note that Base does **not** derive from IContract
public abstract class Base
{
public void Func()
{
Console.WriteLine("Base.Func");
}
}
// Note that Derived does *not* provide implementation for IContract
public class Derived : Base, IContract
{
}
What happens is that Derived magically picks-up a public method Base.Func and decides that it will implement IContract.Func.
What is the reason behind this magic?
IMHO: this "quasi-implementation" feature is very-unintuitive and make code-inspection much harder. What do you think?
© Stack Overflow or respective owner