我刚刚注意到重载决议的一个奇怪行为。
假设我有以下方法:
public static void DoSomething<T>(IEnumerable<T> items)
{
// Whatever
// For debugging
Console.WriteLine("DoSomething<T>(IEnumerable<T> items)");
}
现在,我知道通常会使用少量显式参数调用此方法,因此为方便起见,我添加了此重载:
public static void DoSomething<T>(params T[] items)
{
// Whatever
// For debugging
Console.WriteLine("DoSomething<T>(params T[] items)");
}
现在我尝试调用这些方法:
var items = new List<string> { "foo", "bar" };
DoSomething(items);
DoSomething("foo", "bar");
但在这两种情况下,params
的过载叫做。我会期待 IEnumerable<T>
在 List<T>
的情况下调用重载,因为它似乎更匹配(至少对我而言)。
这种行为正常吗?谁能解释一下?我在 MSDN 文档中找不到关于此的任何明确信息...这里涉及的重载解析规则是什么?
最佳答案
C# 3.0 规范的第 7.4.3 节是此处的相关位。基本上参数数组被扩展了,所以你在比较:
public static void DoSomething<T>(T item)
和
public static void DoSomething<T>(IEnumerable<T> item)
T
第一场比赛被推断为 List<string>
和 T
第二场比赛被推断为string
.
现在考虑参数类型到参数类型的转换——第一个是List<string>
至 List<string>
;第二个是List<string>
至 IEnumerable<string>
.根据 7.4.3.4 中的规则,第一次转换优于第二次转换。
违反直觉的一点是类型推断。如果您将其排除在等式之外,它将按您预期的方式工作:
var items = new List<string> { "foo", "bar" };
DoSomething<string>(items);
DoSomething<string>("foo", "bar");
此时,每次调用中只有一个适用的函数成员。
关于c# - 重载、泛型类型推断和 'params' 关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1820551/