我得到这些扩展:为什么扩展方法调用不明确?
internal static TResult With<TInput, TResult>
(this TInput? o, Func<TInput, TResult> selector, TResult defaultResult = null)
where TInput : struct
where TResult : class
{
selector.ThrowIfNull("selector");
return o.HasValue ? selector(o.Value) : defaultResult;
}
internal static TResult? With<TInput, TResult>
(this TInput? o, Func<TInput, TResult> selector, TResult? defaultResult = null)
where TInput : struct
where TResult : struct
{
selector.ThrowIfNull("selector");
return o.HasValue ? selector(o.Value) : defaultResult;
}
第一个是在参考输入结果和上一个结构的可空的第二个取向。
那么现在为什么在第一行我得到编译错误,第二我没有?
1.
TimeSpan? time = ((int?)4).With(T => TimeSpan.FromSeconds(T))
// Error. The call is ambiguous.
2.
TimeSpan? time = ((int?)4).With(T => TimeSpan.FromSeconds(T), null)
// No errors. Normally calls the second extension.
是不是obvius该时间跨度(作为TResult
)是一个结构,这是在每个延伸的最顶部规定?
好吧,不是一部分。但是为什么当我在最后加上“null”时,编译器就清楚了? – AgentFire 2012-07-29 16:49:34