안녕
나는 일반적이고 널 (NULL) 사용하여이 코드를 가지고 :일반 유형 매개 변수 및 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);
나는 어떤 단서도 없어. 첫 번째는 좋아 보이지만 오류는 컴파일됩니다. 두 번째 것 ('Return')은 첫 번째 것이고 성공적으로 컴파일되면 오류가 발생합니다. 내가 뭐 놓친 거 없니? 일반 메소드 내
스타일 주석처럼 이미 범위에있는 변수와 동일한 이름을 람다 매개 변수에 사용하지 않아야합니다. –
아아 오른쪽, 실제로 컴파일 오류가 발생합니다. 나는 단지 코드를 급하게 붙여 넣고 편집한다. –