49

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).

1
  • @Anthony Pegram: 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). Commented Feb 11, 2012 at 23:38

3 Answers 3

77

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.

3
  • 4
    Yes I completely missed that the <T> is optional. Thank you (and others) for the help. Commented Feb 11, 2012 at 23:42
  • @anthony How can I do this with .NET Framework prior 3.5. There is no delegate Action. Only delegate Action<T>.
    – phse
    Commented Aug 26, 2014 at 9:07
  • @ph1983, prior to C# 3, you would use delegates, though C# 2.0 added support for anonymous methods. I do not have an example question right offhand, but you can read about the feature on MSDN. Commented Aug 26, 2014 at 16:06
26

Yes, there are different overloads of Action taking a different number of input parameters and having a void return type.

Action               public delegate void Action()
Action<T>            public delegate void Action<T>(T obj)
Action<T1,T2>        public delegate void Action<T1,T2>(T1 arg1, T2 arg2)
Action<T1,T2,T3>     public delegate void Action<T1,T2,T3>(T1 arg1, T2 arg2, T3 arg3)
...

The first line is what you are looking for.

Newer Framework versions have added overloads with even more arguments. Maximum number of arguments:

  • .NET Framework 2.0:   1
  • .NET Framework 3.5:   4
  • .NET Framework 4.0: 16
  • Silverlight:                   16

Actions have always a void return type. A void return type need not and cannot be specified as generic type parameter. By contrast, the Func delegates define "real" return types and have always at least one generic type parameter for the return type. Refer here

Func<TResult>           public delegate TResult Func<TResult>()
Func<T,TResult>         public delegate TResult Func<T,TResult>(T arg)
Func<T1,T2,TResult>     public delegate TResult Func<T1,T2,TResult>(T1 arg1, T2 arg2)
...

.NET Framework 4.0 has added in and out modifiers to the generic type parameters for contravariance and covariance. See: Covariance and Contravariance in Generics. Examples:

public delegate void Action<in T1, in T2>(T1 arg1, T2 arg2)

public delegate TResult Func<in T1, in T2, out TResult>(T1 arg1, T2 arg2)
5

What you're looking for is an Action. It takes no parameters, and returns no value.

Not the answer you're looking for? Browse other questions tagged or ask your own question.