I was surprised when I came across this situation and realized that I did not know what the best solution was.
Let's say I have three types:
class A { } class B : A { } class C : A { }
And the following three methods:
DoSomething(A a){ } DoSomething(B b){ } DoSomething(C c){ }
I have a List<A> that contains objects of type B and C
I would like to do this:
foreach(A a in list) { DoSomething(a) }
and call the method that most closely matches the base type, but of course it will always call DoSomething(A a)
I would rather not have a bunch of type checking to get the correct method call, and I don't want to add anything to classes A, B or C.
Is it possible?
source share