void Func without arguments
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)
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.