There are a few posts already on stack overflow about this sort of thing but not exactly the same - so apologies in advance if this is something that has already been answered.
Why does this not work:
public class MyBase { }
public class MyUtils
{
public bool Foo<T> (T myObject) { return true; }
public bool Foo (MyBase myBaseObject) { return false; }
public void Go<T> (IEnumerable<T> items)
{
foreach (var item in items)
{
// this test fails
Assert.IsFalse (Foo (item));
}
}
}
If I call Go() above and pass in a load of MyBase objects, each call to Foo will call the generic Foo (), which returns true.
new MyUtils ().Go (new MyBase[] { new MyBase (), new MyBase () });
Why does it not call the specialised MyBase version instead? If I call Foo (new MyBase ()) directly, it correctly infers which call to make. Is this because of the lack of covariance for collections in C#3, or am I just being silly and not doing this correctly?
Thanks!
Isaac