void Func without arguments

Michel Keijzers picture Michel Keijzers · Feb 12, 2012 · Viewed 57.1k times · Source

There are some similar questions but not exactly like mine.

Is there a Func equivalent for a function without a return value (i.e. void) and without parameters?

The related question is Func not returning anything? but this does not answer for a void type.

(I need it to request actions from my view model to my view).

Answer

Anthony Pegram picture Anthony Pegram · Feb 12, 2012

Your wording is confusing. You perhaps mean "a function without a return type and no parameters." There is simply System.Action.

Action action = () => Console.WriteLine("hello world");
action();

From your comment:

But I need to fill in a <T> type in an Action and void is not a possibility (I will edit my question, I made a mistake).

This indicates a misunderstanding. The T in the Action delegate is an input. The void is the output. An Action delegate is inherently a delegate returning void. The T is the type of input it can act upon, the parameters you would then supply with arguments.

At any rate, as this answer and others show, you can have an Action delegate without any T, a delegate that takes no inputs.