C# 无返回类型的 Func 委托
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/917551/
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
Func delegate with no return type
提问by Marcel Lamothe
All of the Func delegates return a value. What are the .NET delegates that can be used with methods that return void?
所有 Func 委托都返回一个值。可以与返回 void 的方法一起使用的 .NET 委托是什么?
采纳答案by Jason
All Func delegates return something; all the Action delegates return void.
所有 Func 代表都返回一些东西;所有的 Action 委托都返回 void。
Func<TResult>
takes no arguments and returns TResult:
Func<TResult>
不接受任何参数并返回 TResult:
public delegate TResult Func<TResult>()
Action<T>
takes one argument and does not return a value:
Action<T>
接受一个参数并且不返回值:
public delegate void Action<T>(T obj)
Action
is the simplest, 'bare' delegate:
Action
是最简单的“裸”委托:
public delegate void Action()
There's also Func<TArg1, TResult>
and Action<TArg1, TArg2>
(and others up to 16 arguments). All of these (except for Action<T>
) are new to .NET 3.5 (defined in System.Core).
还有Func<TArg1, TResult>
和Action<TArg1, TArg2>
(以及其他多达 16 个参数)。所有这些(除了Action<T>
)都是 .NET 3.5(在 System.Core 中定义)的新内容。
回答by JaredPar
Try System.Func<T>
and System.Action
尝试System.Func<T>
和System.Action
回答by Marcel Lamothe
... takes no arguments and has a void return type?
... 没有参数并且有一个 void 返回类型?
I believe Action
is a solution to this.
我相信这Action
是一个解决方案。
回答by Joel Coehoorn
All of the Func delegates take at least one parameter
所有 Func 委托都至少接受一个参数
That's not true. They all take at least one type argument, but that argument determines the return type.
这不是真的。它们都至少采用一个类型参数,但该参数决定了返回类型。
So Func<T>
accepts no parameters and returns a value. Use Action
or Action<T>
when you don't want to return a value.
所以不Func<T>
接受任何参数并返回一个值。当您不想返回值时使用Action
或Action<T>
。
回答by AndyG
Occasionally you will want to write a delegate for event handling, in which case you can take advantage of System.EvenHandler<T>
which implicitly accepts an argument of type object
in addition to the second parameter that should derive from EventArgs
. EventHandlers will return void
有时候,你会想要写一个事件处理的委托,在这种情况下,你可以利用System.EvenHandler<T>
它隐含接受类型的参数object
,除了应该从获得的第二个参数EventArgs
。EventHandlers 将返回void
I personally found this useful during testing for creating a one-off callback in a function body.
我个人发现这在测试在函数体中创建一次性回调时很有用。
回答by Aarón Iba?ez Wertherm?nn
A very easy way to invoke return and non return value subroutines. is using Funcand Actionrespectively. (see also https://msdn.microsoft.com/en-us/library/018hxwa8(v=vs.110).aspx)
调用返回和非返回值子例程的非常简单的方法。分别使用Func和Action。(另见https://msdn.microsoft.com/en-us/library/018hxwa8(v=vs.110).aspx)
Try this this example
试试这个例子
using System;
public class Program
{
private Func<string,string> FunctionPTR = null;
private Func<string,string, string> FunctionPTR1 = null;
private Action<object> ProcedurePTR = null;
private string Display(string message)
{
Console.WriteLine(message);
return null;
}
private string Display(string message1,string message2)
{
Console.WriteLine(message1);
Console.WriteLine(message2);
return null;
}
public void ObjectProcess(object param)
{
if (param == null)
{
throw new ArgumentNullException("Parameter is null or missing");
}
else
{
Console.WriteLine("Object is valid");
}
}
public void Main(string[] args)
{
FunctionPTR = Display;
FunctionPTR1= Display;
ProcedurePTR = ObjectProcess;
FunctionPTR("Welcome to function pointer sample.");
FunctionPTR1("Welcome","This is function pointer sample");
ProcedurePTR(new object());
}
}
回答by mojmir.novak
... takes no arguments and has a void return type?
... 没有参数并且有一个 void 返回类型?
If you are writing for System.Windows.Forms
, You can also use:
如果您正在为 写作System.Windows.Forms
,您还可以使用:
public delegate void MethodInvoker()