Привет
У меня есть этот код, использующий общий и допускающий значение NULL:
// 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, одно - это класс, другое - структура. Затем я использую их в:
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);
Я не понимаю, почему это произошло. Первый вроде нормально, но компилирует ошибку. Второй («Возврат») должен быть ошибкой, если первый есть, но компилируется успешно. Я что-то пропустил?