혼란

2009-09-05 2 views
8
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 표현식과 관련이 없습니까? 아니면 잘못된 것을하고 있습니까?혼란

답변

18

앞에서 말씀 드린대로 Action에는 매개 변수가 없습니다. 당신이 할 경우이 : 그 방법 매개 변수되지 않도록 당신은 실제로 여기에 방법을 실행

var stringAction = new System.Action(StringAction("a string")); 

.

당신이 할 경우이 :

var stringAction = new System.Action(param => StringAction("a string")); 

당신이 당신의 방법은 행동하지 않는 param라는 매개 변수를 소요를 말한다.

그래서 올바른 방법이 될 것이라고해야 할 일 :

var stringAction = new System.Action(() => StringAction("a string")); 

이상의 콤팩트 : 빈 괄호는 매개 변수를 고려하지 않습니다 람다를 나타내는 데 사용됩니다

Action stringAction =() => StringAction("a string"); 

.

+0

@Botz :
new Action<string>(myStringParam => StringAction(myStringParam));

따라서, 귀하의 경우, 전체 코드는 다음과 같이 할 분 ("문자열");'(컴파일러는'var'이'System.Action '이라는 것을 알기에 충분한 정보를 가지고 있지 않다.) "System.Action stringAction = '). – devuxer

+0

오, 고마워. 그것을 수정했습니다. – Botz3000

2

C# 2.0에서 Action 대리자는 매개 변수를 허용하지 않는 void 대리자입니다. 이후 버전에는 일반 Action<T> 대리자가 있습니다. 여기서 T는 매개 변수 유형을 지정합니다. 당신은 내가이에 대한 전문가가 아니에요

stringAction("Hello world"); 
4

를 호출 할 수 있습니다, 다음

var stringAction = new Action<string>(StringAction); // using method group conversion 

,하지만 당신은 :

var stringAction = new Action<string>(param => StringAction(param)); 

또는 더 나은 :

이 작업을해야합니다 이것을 시도 했는가?

4

Action 대리자는 매개 변수가없고 void를 반환하는 메서드에 대한 대리자로 정의됩니다. 샘플 1에서 실수를 두 번하고 있습니다 :
1. 메서드를 호출하려고합니다. 매개 변수는
입니다. 2. 메서드를 호출하고 매개 변수로 제공하지 않습니다 (새 Action (methodName)이어야 함)), 1로 인해 작동하지 않을 수도 있습니다.샘플 2에서

, 당신은 당신의 람다 매개 변수를 취하고, 다시 같은 실수를하고있다, 당신은 이런 식으로 작성해야 : 당신이 대리자를 만들려면
new Action(() => StringAction("a string"));

, 즉 매개 변수를 취할 것 , 당신은 이런 식으로 그것을 수행해야합니다


private void StringAction(string aString) // method to be called 
{ 
    return; 
} 

private void TestDelegateStatement1() // now it works 
{ 
    var stringAction = new Action<string>(StringAction); 
    //You can call it now: 
    stringAction("my string"); 
} 

private void TestDelegateStatement2() // now it works 
{ 
    var stringAction =() => StringAction("a string"); 
    //Or the same, with a param: 
    var stringActionParam = (param) => StringAction(param); 

    //You can now call both: 
    stringAction(); 
    stringActionParam("my string"); 
} 

private void TestDelegateStatement3() // this is ok 
{ 
    var stringAction = new System.Action(StringActionCaller); 

    stringAction(); 
} 

private void StringActionCaller() 
{ 
    StringAction("a string"); 
}