115
votes

I can't understand why the following C# code doesn't compile.

As you can see, I have a static generic method Something with an IEnumerable<T> parameter (and T is constrained to be an IA interface), and this parameter can't be implicitly converted to IEnumerable<IA>.

What is the explanation? (I don't search for a workaround, just to understand why it doesn't work).

public interface IA { }
public interface IB : IA { }
public class CIA : IA { }
public class CIAD : CIA { }
public class CIB : IB { }
public class CIBD : CIB { }

public static class Test
{
    public static IList<T> Something<T>(IEnumerable<T> foo) where T : IA
    {
        var bar = foo.ToList();

        // All those calls are legal
        Something2(new List<IA>());
        Something2(new List<IB>());
        Something2(new List<CIA>());
        Something2(new List<CIAD>());
        Something2(new List<CIB>());
        Something2(new List<CIBD>());
        Something2(bar.Cast<IA>());

        // This call is illegal
        Something2(bar);

        return bar;
    }

    private static void Something2(IEnumerable<IA> foo)
    {
    }
}

Error I get in Something2(bar) line:

Argument 1: cannot convert from 'System.Collections.Generic.List' to 'System.Collections.Generic.IEnumerable'

2
You have not restricted T to reference types. If you use the condition where T: class, IA then it should work. The linked answer has more details. - Dirk
@Dirk I don't think this should be flagged as a duplicate. While it's true that the concept problem here is a covariance/contravariance problem in the face of value types, the specific case here is "what does this error message mean" as well as the author not realizing merely including "class" fixes his issue. I believe future users will search for this error message, find this post, and leave happy. (As I often do.) - Reginald Blue
You can also reproduce the situation by simply saying Something2(foo); directly. Going around .ToList() to get a List<T> (T is your type parameter declared by the generic method) is not needed to understand this (a List<T> is an IEnumerable<T>). - Jeppe Stig Nielsen
@ReginaldBlue 100%, was going to post the same thing. Similar answers do not a duplicate question make. - StayOnTarget

2 Answers

219
votes

The error message is insufficiently informative, and that is my fault. Sorry about that.

The problem you are experiencing is a consequence of the fact that covariance only works on reference types.

You're probably saying "but IA is a reference type" right now. Yes, it is. But you didn't say that T is equal to IA. You said that T is a type which implements IA, and a value type can implement an interface. Therefore we do not know whether covariance will work, and we disallow it.

If you want covariance to work you have to tell the compiler that the type parameter is a reference type with the class constraint as well as the IA interface constraint.

The error message really should say that the conversion is not possible because covariance requires a guarantee of reference-type-ness, since that is the fundamental problem.

26
votes

I just wanted to complement Eric's excellent insider answer with a code example for those that may not be that familiar with generic constraints.

Change Something's signature like this: The class constraint has to come first.

public static IList<T> Something<T>(IEnumerable<T> foo) where T : class, IA