I see this line in many online examples of using the Action delegate:
public event Action MyEvent;
But when I try it in my own code, I get this error
Using the generic type 'System.Action' requires '1' type arguments
The documentation certainly describes a form of Action without any type parameter. What am I missing?
You can use the Action<T> delegate to pass a method as a parameter without explicitly declaring a custom delegate. The encapsulated method must correspond to the method signature that is defined by this delegate.
Because the instantiated delegate is an object, it can be passed as an argument, or assigned to a property. This allows a method to accept a delegate as a parameter, and call the delegate at some later time.
An Action type delegate is the same as Func delegate except that the Action delegate doesn't return a value. In other words, an Action delegate can be used with a method that has a void return type. It can contain minimum 1 and maximum of 16 input parameters and does not contain any output parameter.
The Action delegate is generally used for those methods which do not contain any return value, or in other words, Action delegate is used with those methods whose return type is void. It can also contain parameters of the same type or of different types.
Expanding on Andrews answer.
It's perfectly legal to use Action in a non-3.5 scenario. Simply define it yourself.
public delegate void Action();
Make sure your application is referencing System.Core
.
Edit - also make sure you are targeting .NET 3.5 as the System.Core.dll is part of that version.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With