Method hiding is a technique used to replace a non-virtual method of a base class with new functionality in the child class. It has some nasty behaviour that can easily catch people out. To explain by way of an example:
class BaseClass
{
public void Foo()
{
Console.WriteLine("BaseClass");
}
}
class ChildClass : BaseClass
{
public new void Foo()
{
Console.WriteLine("ChildClass");
}
}
ChildClass obj1 = new ChildClass();
BaseClass obj2 = obj1;
obj1.Foo(); // Prints "ChildClass"
obj2.Foo(); // Prints "BaseClass"
If Foo
had been declared virtual
, new
would not be needed and both obj1.Foo()
and obj2.Foo()
would have printed ChildClass
in the example above.
The only other thing you need to know about it is that these days using inheritance is generally frowned upon (do a search of either "inheritance is evil" or "composition vs inheritance" for reams of info on why this is). You therefore shouldn't need to worry about method hiding (unless someone inflicts it upon you with their old and/or misguided code).