Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

action delegate with zero parameters

Tags:

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?

like image 328
I. J. Kennedy Avatar asked Feb 27 '09 01:02

I. J. Kennedy


People also ask

How do you use an Action delegate?

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.

Can we pass delegate as parameter?

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.

What is the difference between Func and Action delegate?

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.

What is the use of Action delegate in C#?

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.


2 Answers

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(); 
like image 73
JaredPar Avatar answered Oct 10 '22 17:10

JaredPar


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.

like image 27
Andrew Hare Avatar answered Oct 10 '22 17:10

Andrew Hare