开发者

C# overloading with generics: bug or feature?

Let's have a following simplified example:

void Foo<T>(IEnumerable<T> collection, params T[] items) 
{
    // ...
}开发者_运维知识库

void Foo<C, T>(C collection, T item)
    where C : ICollection<T>
{
    // ...
}

void Main()
{
    Foo((IEnumerable<int>)new[] { 1 }, 2);
}

Compiler says:

The type 'System.Collections.Generic.IEnumerable' cannot be used as type parameter 'C' in the generic type or method 'UserQuery.Foo(C, T)'. There is no implicit reference conversion from 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.ICollection'.

If I change Main to:

void Main()
{
    Foo<int>((IEnumerable<int>)new[] { 1 }, 2);
}

It will work ok. Why compiler does not choose the right overload?


My guess is that the compiler chooses the best match before it uses the generic constraint. In your example the method with the constraint is preferable because it doesn't have a params last parameter.

Edit - Eric Lippert confirms this in his answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜