Overload resolution issue for generic method with constraints

Does compiler take into account type parameter constraints at all, when resolving overloads?

No, because generic constraints are not part of the function signature. You can verify this by adding a Bar overload that is identical except for the generic constraints:

interface IBar { }

static void Bar<T>(IEnumerable<T> value)
    where T : IFoo
{
}

static void Bar<T>(T source)
    where T : IBar
{
    // fails to compile : Type ____ already defines a member called 'Bar' with the same parameter types
}

The reason your code doesn't compile is because the compiler chooses the "best" match based on the method signature, then tries to apply the generic constraints.

One possible reason why it doesn't is because this call would be ambiguous:

{suppose List<T> had an Add<T>(IEnumerable<T> source) method}

List<object> junk = new List<object>();
junk.Add(1);   // OK
junk.Add("xyzzy") // OK
junk.Add(new [] {1, 2, 3, 4});  //ambiguous - do you intend to add the _array_ or the _contents_ of the array?

The obvious fix is to use a different name for the Bar method that takes a collection (as is done in the BCL with Add and AddRange.


EDIT: Ok, the reason why Bar<T>(T source) is selected over Bar<T>(IEnumerable<T> source) when passing an List is because of the "7.5.3.2 Better function member" section of the C# language reference. What it says is that when an overload resolution must occur, the arguments types are matched to the parameters types of the applicable function members (section 7.5.3.1) and the better function member is selected by the following set of rules:

• for each argument, the implicit conversion from EX to QX is not better than the implicit conversion from EX to PX, and

• for at least one argument, the conversion from EX to PX is better than the conversion from EX to QX.

(PX being the parameter types of the first method, QX of the second one)

This rule is applied "after expansion and type argument substitution". Since type argument substitution will swap the Bar(T source) to Bar>(IList source), this method arguments will be a better match than the Bar(IEnumerable source) which needs a conversion.

I couldn't find a online version of the language reference, but you can read it here


EDIT: misunderstood the question initially, working on finding the correct answer in the c# language spec. Basically IIRC the method is selected by considering the most appropriate type, and if you don't cast your parameter to IEnumerable<> exactly, then the Bar<T>(T source) will match the parameter type exactly, just like in this sample:

public interface ITest { }
public class Test : ITest { }

private static void Main(string[] args)
{
    test(new Test() ); // outputs "anything" because Test is matched to any type T before ITest
    Console.ReadLine();
}


public static void test<T>(T anything)
{
    Console.WriteLine("anything");
}

public static void test(ITest it)
{
    Console.WriteLine("it");
}

Will link to it when found


Because the cast between an array and an enumerable must be explicit: this compiles

var value = new FooImpl[0].AsEnumerable();
Bar(value);

and so does this:

var value = new FooImpl[0] as IEnumerable<IFoo>;
Bar(value);

From the doc:

Starting with the .NET Framework 2.0, the Array class implements the System.Collections.Generic.IList, System.Collections.Generic.ICollection, and System.Collections.Generic.IEnumerable generic interfaces. The implementations are provided to arrays at run time, and as a result, the generic interfaces do not appear in the declaration syntax for the Array class.

So your compiler doesn't know that the array matches the signature for Bar, and you have to explicitly cast it