Confusão sobre expressões delegadas e lambda do `Action`
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");
}
Eu não entendo porqueTestDelegateStatement3
funciona masTestDelegateStatement1
falha. Em ambos os casos,Action
é fornecido com um método que usa zero parâmetros. Eles podemligar um método que usa um único parâmetro (aString
), mas isso deve ser irrelevante. Eles não aceitam um parâmetro. Isso não é possível com expressões lamda, ou estou fazendo algo errado?