C# compiler fails to recognize a class is implementing an interface
Posted
by Freek
on Stack Overflow
See other posts from Stack Overflow
or by Freek
Published on 2010-05-20T08:50:01Z
Indexed on
2010/05/20
8:50 UTC
Read the original article
Hit count: 159
c#
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
}
}
© Stack Overflow or respective owner