我得到這些擴展:爲什麼擴展方法調用不明確?
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