2011-04-20 2 views
3

다음과 같이 선언 된 유형이 주어짐문제 해결 : 여기에 형식 유추가 실패하지 않는 이유는 무엇입니까?

public class EqualityProbe<T> 
{ 
    public EqualityProbe(Func<T> functionToGetActualValue, T expectedValue, string probeDescription) {..} 

클라이언트 코드 :

// cannot infer bool here 
new EqualityProbe(CanConnectToMachine, true, "Probe machine is online") 
// compiles fine 
new EqualityProbe<bool>(CanConnectToMachine, true, "Probe machine is online") 

필자의 생각은 메소드 그룹 (예 : CanConnectToMachine) 또는 익명 메소드 (람다 표현식) .
그러나이 경우 컴파일러가 두 번째 인수

에서 형식 인수를 유추하지 않는 이유는 무엇입니까?
+0

[C# 생성자가 추론 할 수없는 이유는 무엇입니까?] (http://stackoverflow.com/questions/3570167/why-cant-the-c-constructor-infer-type) – AakashM

+0

가능한 복제본 [ 왜 C#은 클래스 생성자에 함축 된 제네릭 형식을 지원하지 않습니까?] (http://stackoverflow.com/questions/45604/why-doesnt-c-sharp-support-implied-generic-types-on-class-constructors) – nawfal

답변

1

음, C#을인지하는 방법! 생성자에 대해서는 추론하지 않습니다. 제네릭 형식을 인스턴스화하는 동안 제네릭 형식에 사용할 형식을 지정해야합니다.

관련 문제