Possible Duplicate:
method hiding in c# with a valid example .why is it implemented in the framework.? what is the Real world advantage.?
I am wondering how method hiding could be useful in c# ? If you dive into the concept ,you can find that method hiding is something confusing and drives us to spaghetti code style.There are some reasons for that :
1- If you need a fresh method in your Derived class you can define a new method with a new name in your derived class.
2-In a polymorphic manner usage when you use upcasting, the expected behavior would not be achieved and this could be confusing.
(Specially this strange behavior could be so apparent and obvious if the Base class and the Derived Class were in the different libraries and you want to have a polymorphic usage of them in somewhere in your code)
class Base
{
public void Method() { Console.WriteLine("I am Base Class"); }
}
class Derived: Base
{
public new void Method() { Console.WriteLine("I am Derived Class");}
}
class Program
{
static void Main(string[] args)
{
Base C = new Derived();
C.Method(); //output:"I am Base Class"
}
}
Such a behavior could be confusing, but If we used "virtual" in "Base" and "override" in "Derived" (I mean a polymorphic usage) the output was: "I am Derived Class". and this output is what that I think the most of us expected.
In other words I believe that in a hierarchy, most of developers expected for overridden methods in a derived class Not New methods that wanna hide the base class implementation.
I cant understand why such a bad thing that has no advantages there is in c# language? at least I want to know what is the best usages of method hiding in c# ?
thank you for your help