Is it possible to declare generic delegate with no parameters?

Exitos picture Exitos · Dec 7, 2011 · Viewed 13.2k times · Source

I have...

Func<string> del2 = new Func<string>(MyMethod);

and I really want to do..

Func<> del2 = new Func<>(MyMethod);

so the return type of the callback method is void. Is this possible using the generic type func?

Answer

svick picture svick · Dec 7, 2011

The Func family of delegates is for methods that take zero or more parameters and return a value. For methods that take zero or more parameters an don't return a value use one of the Action delegates. If the method has no parameters, use the non-generic version of Action:

Action del = MyMethod;