Can anyone tell me why this does not work? I would have thought the constraint would make it valid.
public class ClassA<T> where T : ICommon
{
public ClassA()
{
ClassB b = new ClassB();
IEnumerable<T> alist = new List<T>;
b.Items = alist;
//Error: cannot convert from IEnumerable<T> to IEnumerable<ICommon>'
}
}
public class ClassB
{
public IEnumerable<ICommon> Items { get; set;}
....
}
This will work in C# 4 with a tweak, but not in C# 3. What you’re looking for is generic variance, which has been supported in the CLR since .NET 2.0, but not in C# until v4.
Even in C# 4, you need to also constrain T to be a reference type – as covariance doesn’t work over value types. For example,
List<int>can’t be converted toIEnumerable<IComparable>even thoughintimplementsIComparable.Having tweaked your code a bit (there were a few typos, effectively), this compiles with the C# 4 compiler:
If you’re stuck in C# 3 and .NET 3.5, an alternative would be to use
Cast<T>():