This post originated from an RSS feed registered with .NET Buzz
by Roy Osherove.
Original Post: A simple question about overloading
Feed Title: ISerializable
Feed URL: http://www.asp.net/err404.htm?aspxerrorpath=/rosherove/Rss.aspx
Feed Description: Roy Osherove's persistent thoughts
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Roy Osherove
Latest Posts From ISerializable
Advertisement
Given 3 classes:
PersonBase, Employee and Manager:
class
PersonBase { }
class
Employee : PersonBase { }
class
Manager : Employee { }
and given the
following methods:
public
void Loop()
{
PersonBase [] people = new PersonBase []{
new PersonBase (),
new Employee (),
new Manager ()
};
foreach (PersonBase pb in
people)
{
doSomething(pb);
}
}
void doSomething(PersonBase p)
{
Console .WriteLine("PersonBase" );
}
void doSomething(Employee e)
{
Console .WriteLine("Employee" );
}
void doSomething(Manager m)
{
Console .WriteLine("Manager" );
}
Why does only the
first of the three âdoSomethingâ overloads get called for all the objects in the
array?
How could you
force those methods to get called?
(Ignore the
various class factory solutions out there.)
Read: A simple question about overloading