c# - 泛型类型参数和 Nullable 方法重载

标签 c# .net generics types nullable

你好
我有使用通用和可为空的代码:

// The first one is for class
public static TResult With<TInput, TResult>(this TInput o, 
          Func<TInput, TResult> evaluator)
    where TResult : class
    where TInput : class

// The second one is for struct (Nullable)
public static TResult With<TInput, TResult>(this Nullable<TInput> o, 
          Func<TInput, TResult> evaluator)
    where TResult : class
    where TInput : struct

请注意TInput约束,一个是class,一个是struct。然后我将它们用于:

string s;
int? i;

// ...

s.With(o => "");
i.With(o => ""); // Ambiguos method

它会导致 Ambiguos 错误。但我还有另一对:

public static TResult Return<TInput, TResult>(this TInput o,
          Func<TInput, TResult> evaluator, TResult failureValue)
    where TInput : class

public static TResult Return<TInput, TResult>(this Nullable<TInput> o,
          Func<TInput, TResult> evaluator, TResult failureValue)
    where TInput : struct

这个编译成功

string s;
int? i;

// ...

s.Return(o => 1, 0);
i.Return(o => i + 1, 0);

我不知道为什么会这样。第一个似乎没问题,但编译错误。如果第一个错误,第二个('Return')应该是错误的,但编译成功。我错过了什么吗?

最佳答案

选择重载时不考虑泛型方法中的约束 - 在选择重载之后检查它们。

作为选择重载的一部分,检查参数类型内的约束。这有点令人困惑,但最终还是有道理的。

我有一个 blog post这可能有助于进一步理解它。

另外请注意,您的第二个示例具有有助于类型推断的附加参数,这就是两者之间的区别。 TResult推断为 int ,这会阻止第一个重载有效 - 没有来自 (int? x) => x + 1 的转换至 Func<int?, int> 是从 (int x) => x + 1 的转换至 Func<int, int> .

关于c# - 泛型类型参数和 Nullable 方法重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5814842/

相关文章:

c# - 如何在 NHibernate QueryOver 查询中选择和使用值对象的集合

.net - 在 F# 中的 Vector2 上重载相等运算符 ("=") - 不同的向量被报告为相等

java - 如何通过类类型委托(delegate)构造函数的实例化?

c# - 如何设置可用于 ResourceDictionary 中背景属性的原色

c# - 回收调用Application_Start吗?

c# - 使用发布-订阅模式通过 SignalR 向客户端发送更新

c# - 不等待 HttpClient 使用时会发生什么

.net - 在 Visual Studio 监 window 口中更改 DateTime 的默认显示

java - 继承带有边界扩展的通用内部类的编译错误

delphi - 对 TDictionary 进行排序