我得到了这些扩展名:
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;
}
第一个面向引用类型的结果,第二个面向结构的Nullable。
那么,现在为什么在第一行中出现编译错误而在第二行中却没有编译错误?
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.
难道TimeSpan(作为
TResult
)是在每个扩展名的最顶部指定的结构吗? 最佳答案