C# compiler fails to recognize a class is implementing an interface
- by Freek
The following code fails to compile (using VS2010) and I don't see why. The compiler should be able to infer that List is 'compatible' (sorry for lack of a better word) with IEnumerable, but somehow it doesn't. What am I missing here?
interface ITest {
void Test();
}
class TestClass : ITest {
public void Test() {
}
}
class Program {
static void Test(IEnumerable<ITest> tests) {
foreach(var t in tests) {
Console.WriteLine(t);
}
}
static void Main(string[] args) {
var lst = new List<TestClass>();
Test(lst); // fails, why?
Test(lst.Select(t=>t as ITest)); //success
Test(lst.ToArray()); // success
}
}