C# void Func 不带参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9244989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 06:31:25  来源:igfitidea点击:

void Func without arguments

c#wpfmvvmfunc

提问by Michel Keijzers

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?

对于没有返回值(即 void)且没有参数的函数,是否有 Func 等价物?

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

相关的问题是Func 没有返回任何东西?但这并不能回答 void 类型。

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

(我需要它来请求从我的视图模型到我的视图的操作)。

采纳答案by Anthony Pegram

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

你的措辞令人困惑。您的意思可能是“没有返回类型和参数的函数”。只是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).

但是我需要<T>在 Action 中填写一个类型并且 void 是不可能的(我将编辑我的问题,我犯了一个错误)。

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 inputit can act upon, the parameters you would then supply with arguments.

这是一种误解。Action 委托中的 T 是一个input。空是输出。Action 委托本质上是一个返回 void 的委托。T 是它可以操作的输入类型,然后您将提供参数的参数。

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

无论如何,正如这个答案和其他答案所示,您可以拥有一个没有任何 T 的 Action 委托,一个不需要输入的委托。

回答by RyanR

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

你正在寻找的是一个动作。它不接受任何参数,也不返回任何值。

回答by Olivier Jacot-Descombes

Yes, there are different overloads of Actiontaking a different number of input parameters and having a voidreturn type.

是的,Action采用不同数量的输入参数并具有void返回类型有不同的重载。

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
  • .NET 框架 2.0:   1
  • .NET 框架 3.5:   4
  • .NET 框架 4.0:16
  • 银光:                   16


Actions have always a voidreturn type. A voidreturn type needs not and cannot be specified as generic type parameter. By contrast, the Funcdelegates define "real" return types and have always at least one generic type parameter for the return type:

动作总是有一个void返回类型。一个void返回类型不需要和不能被指定为一般类型参数。相比之下,Func委托定义了“真正的”返回类型,并且总是至少有一个返回类型的泛型类型参数:

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 inand outmodifiers to the generic type parameters for contravariance and covariance. See: Covariance and Contravariance in Generics. Examples:

.NET Framework 4.0 已向通用类型参数添加inout修饰符以实现逆变和协变。请参阅:泛型中的协方差和逆变。例子:

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)