简体   繁体   English

void Func 不带参数

[英]void Func without arguments

There are some similar questions but not exactly like mine.有一些类似的问题,但不完全像我的。

Is there a Func equivalent for a function without a return value (ie void) and without parameters?对于没有返回值(即 void)且没有参数的函数,是否有 Func 等价物?

The related question is Func not returning anything?相关的问题是Func 没有返回任何东西? but this does not answer for a void type.但这并不能回答 void 类型。

(I need it to request actions from my view model to my view). (我需要它来请求从我的视图模型到我的视图的操作)。

Your wording is confusing.你的措辞令人困惑。 You perhaps mean "a function without a return type and 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).但是我需要在 Action 中填写<T>类型并且 void 是不可能的(我将编辑我的问题,我犯了一个错误)。

This indicates a misunderstanding.这是一种误解。 The T in the Action delegate is an input . Action 委托中的 T 是一个input The void is the output.空是输出。 An Action delegate is inherently a delegate returning void. Action 委托本质上是一个返回 void 的委托。 The T is the type of input it can act upon, the parameters you would then supply with arguments. 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 委托,一个不需要输入的委托。

Yes, there are different overloads of Action taking a different number of input parameters and having a void return 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 框架 2.0: 1
  • .NET Framework 3.5: 4 .NET 框架 3.5: 4
  • .NET Framework 4.0: 16 .NET 框架 4.0: 16
  • Silverlight: 16银光: 16

Actions have always a void return type.操作始终具有void返回类型。 A void return type need not and cannot be specified as generic type parameter. void返回类型不需要也不能指定为泛型类型参数。 By contrast, the Func delegates define "real" return types and have always at least one generic type parameter for the return type.相比之下, Func委托定义了“真正的”返回类型,并且总是至少有一个返回类型的泛型类型参数。 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. .NET Framework 4.0 已向通用类型参数添加了inout修饰符以实现逆变和协变。 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)

What you're looking for is an Action.你正在寻找的是一个动作。 It takes no parameters, and returns no value.它不接受任何参数,也不返回任何值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM