Why can't generic methods choose a specific method when the generic one doesn't match?

Viewed 56

Given the following class:

public static class MWE
{
    public static void Foo<T, TColl>(Expression<Func<IFoo, bool>> bar, TColl foos)
        where TColl : IEnumerable<T>
    {
    }

    public static void Foo<T, TColl>(Expression<Func<IFoo, T>> bar, TColl foos)
        where TColl : IEnumerable<T>
    {
    }

    static MWE()
    {
        Foo(bar => bar.GetThing(), new List<IThing>());
        Foo(bar => bar.GetBool(), new List<IThing>());
    }
}

public interface IFoo
{
    IThing GetThing();

    bool GetBool();
}

I get a compilation error on the second call to Foo, complaining that there's no conversion from List<IThing> to IEnumerable<bool>. It seems to me that the compiler doesn't see the first overload, although it matches (and the second doesn't).

Why? Is there a way these two methods can co-exist and work?

3 Answers
Related