Consider the following code:
interface IFooable
{
void Foo();
}
class Blah : IFooable
{
public void Foo()
{
Console.WriteLine("Hi from 'Blah'");
}
}
class Bar : Blah, IFooable
{
public void Foo()
{
Console.WriteLine("Hi from 'Bar'");
}
}
Compiling this code will give the following warning in the compiler:
'Bar.Foo()' hides inherited member 'Blah.Foo()'. Use the new keyword if hiding was intended.
But 'Foo' is an interface method implementation which, by definition, IIRC, is virtual. If you run the following code:
var fooableList = new List<IFooable>();
fooableList.Add(new Blah());
fooableList.Add(new Bar());
foreach (var fooable in fooableList)
{
fooable.Foo();
}
Surely enough this will output like a virtual call:
Hi from 'Blah'
Hi from 'Bar'
This is quite misleading, moreso if you go ahead and follow the compiler's warning suggestion an mark Bar.Foo
as new
which has no bearing at all in the output of the program. Yes I know I'm making Bar
implement "twice" IFooable
but that's still legal code and the resulting warning is completely wrong.
Am I deeply misunderstanding something on how interfaces and method hiding works or is this a corner case compiler warning glitch?