I´ve got the following classes:
public class Person
{
}
public class Employee : Person
{
}
public class Customer : Person
{
}
Some methods that use these classes:
public class OtherClass
{
public void DoSomething(Employee e)
{
}
public void DoSomething(Customer c)
{
}
}
The call:
// People = Collection<Person>.
foreach (var p in People)
DoSomething(p); // Should call the right method at runtime and "see" if it´s an Employee or a Customer.
The compiler does not allow this. How do I implement this scenario?
The easiest approach here is polymorphism, i.e.
and use:
HOWEVER! If that isn’t possible, then cheat:
Another option would be to check the type yourself: