C# overloading with generics: bug or feature?

天涯浪子 提交于 2019-12-06 22:12:39

问题


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?


回答1:


Your question is answered here.

http://blogs.msdn.com/ericlippert/archive/2009/12/10/constraints-are-not-part-of-the-signature.aspx

Please also read the approximately one million comments telling me that I am wrong for some interesting additional commentary on this issue.




回答2:


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.



来源:https://stackoverflow.com/questions/2586666/c-sharp-overloading-with-generics-bug-or-feature

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!