C# 3.0에서 대리인을 전달하기위한 규칙을 이해하는 데 문제가 있습니다. 최종 .Max()
은 아래에 표시된 것처럼 컴파일 오류를 제공하지만이 사건과 다른 사건 사이에 중요한 차이점이 무엇인지 이해할 수 없습니다.이 명명 된 함수를 C#에서 Func <> 인수로 전달할 수없는 이유는 무엇입니까?
int NamedIdentity(int val) { return val; }
Func<int, int> Identity = x => x;
void Main() {
var range = Enumerable.Range(1, 10);
range.Max();
range.Max(x => x);
range.Max(Identity);
range.Max((Func<int, int>) NamedIdentity);
range.Max(new Func<int, int>(NamedIdentity));
// this line gives compile error
range.Max(NamedIdentity);
/* The call is ambiguous between the following methods or properties:
* 'System.Linq.Enumerable.Max<int>(System.Collections.Generic.IEnumerable<int>, System.Func<int,decimal>)' and
* 'System.Linq.Enumerable.Max<int>(System.Collections.Generic.IEnumerable<int>, System.Func<int,decimal?>)'
*/
}
왜 컴파일되지 않습니까? 내 이해는 NamedIdentity
은 Func<int, int>
으로 선언되었으므로 형변환 할 필요는 없습니다. 분명히 틀렸어. 한 가지 단서는 컴파일 오류가 Decimal
에 대해 이야기하고 있지만 코드의 아무 곳에서나 참조됩니다. 그게 어디서 온거야? 함수 참조가 동일한 서명의 대리자에 암시 적으로 캐스트 될 수 있습니까?
컴파일 오류가 십진수를 언급하는 이유는 무엇입니까? – recursive
@ 재귀 : 나는 이것이 처음 두 가지 과부하이기 때문에 믿습니다. 나는 완전히 확신하지 못한다. – SLaks