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");
}
I don't understand why TestDelegateStatement3
works but TestDelegateStatement1
fails. In both cases, Action
is supplied with a method that takes zero parameters. They may call a method that takes a single parameter (aString
), but that should be irrelevant. They don't take a parameter. Is this just not possible to do with lamda expressions, or am I doing something wrong?
In C# 2.0, the
Action
delegate is avoid
delegate that doesn't accept parameters. In later versions, there's the genericAction<T>
delegate, where T specifies the parameter type.This should work:
or even better:
then, you can call
I'm not an expert on this, but have you tried this?
Action
delegate is defined as delegate to method, that has no parameters and returns void. In sample 1, you are making 2 mistakes:1. You are trying to give method, that takes parameter
2. You are invoking the method, and not giving it as a parameter (it should be new Action(methodName)), although, it wouldn't work because of 1.
In sample 2, you are making the same mistake again, your lambda is taking a parameter, you should write it like this:
new Action(() => StringAction("a string"));
If you want to create a delegate, that will take a parameter, you should do it like this:
new Action<string>(myStringParam => StringAction(myStringParam));
So, in your case, complete code would look like this:
As you said, Action doesn't take any parameters. If you do this:
You actually execute the method here, so that is not a method parameter.
if you do this:
you tell it that your method takes a parameter called
param
, which Action does not.So the correct way to do this would be:
or more compact:
the empty brackets are used to indicate the lambda doesn't take any parameters.