private void StringAction(string aString) // method to be called
{
return;
}
private void TestDelegateStatement1() // doesn't work
{
var stringAction = new System.Action(StringAction("a string"));
// Error: "Method expected"
}
private void TestDelegateStatement2() // doesn't work
{
var stringAction = new System.Action(param => StringAction("a string"));
// Error: "System.Argument doesn't take 1 arguments"
stringAction();
}
private void TestDelegateStatement3() // this is ok
{
var stringAction = new System.Action(StringActionCaller);
stringAction();
}
private void StringActionCaller()
{
StringAction("a string");
}
TestDelegateStatement3
작동하지만 TestDelegateStatement1
실패 이유를 이해하지 않는 식입니다. 두 경우 모두 Action
에는 0 매개 변수를 사용하는 메소드가 제공됩니다. 은 단일 매개 변수 (aString
)를 사용하는 메서드이지만 부적절한 메서드 일 수 있습니다. 매개 변수를 사용하지 않습니다. 이것은 lamda 표현식과 관련이 없습니까? 아니면 잘못된 것을하고 있습니까?혼란
@Botz :
new Action<string>(myStringParam => StringAction(myStringParam));
따라서, 귀하의 경우, 전체 코드는 다음과 같이 할 분 ("문자열");'(컴파일러는'var'이'System.Action '이라는 것을 알기에 충분한 정보를 가지고 있지 않다.) "System.Action stringAction = '). – devuxer
오, 고마워. 그것을 수정했습니다. – Botz3000