Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

void Func without arguments

Tags:

c#

mvvm

wpf

func

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

like image 308
Michel Keijzers Avatar asked Feb 11 '12 23:02

Michel Keijzers


People also ask

What is void function without parameters?

In C, an empty parameter list means that the number and type of the function arguments are unknown. Example: int f(); // means int f(void) in C++ // int f( unknown ) in C. In C, it makes sense to avoid that undesirable "unknown" meaning. In C++, it's superfluous.

Do void functions have arguments?

When used for a function's parameter list, void specifies that the function takes no parameters. When used in the declaration of a pointer, void specifies that the pointer is "universal."

How do you declare a function with no parameters?

In C, use void no_args(void) to declare a function that truly takes no parameters (and returns nothing).

Can we call a function without passing arguments?

to answer your question concerning calling functions without their parameters. it's possible for functions where default values for all parameters are set e.g.: def foo(bar = "default string"): print(bar) print(foo()) # prints: default string print(foo("hello world!")) # prints: hello world!


2 Answers

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.

like image 119
Anthony Pegram Avatar answered Oct 01 '22 09:10

Anthony Pegram


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)
like image 23
Olivier Jacot-Descombes Avatar answered Oct 01 '22 09:10

Olivier Jacot-Descombes