简体   繁体   English

如何创建不返回的C#委托?

[英]How can I create a C# Delegates that does not return?

由于Func <>委托不采用“ void”,我如何在C#3.0中获得以下权限

Func<int, int, void> delg = (a, b) => { Console.WriteLine( a + b); };

使用操作而不是Func。

    Action<int, int> delg = (a, b) => { Console.WriteLine( a + b); };

As an alternative to Action<int, int> you can create your own delegate and return and pass in whatever types you want. 作为Action<int, int>的替代方法Action<int, int>您可以创建自己的委托,然后返回并传递所需的任何类型。

public delegate void MyDelegate(int val1, int val2);

use: 采用:

MyDelegate del = (a, b) => {Console.WriteLine( a + b); };
    Func<int, int, Action> foo = (a, b) => () => Console.Out.WriteLine("{0}.{1}", a, b);
        foo(1, 2)();

Func and Action are delegate wrappers, Funcs return a value (the name might come from VBs function..?) and Actions dont. Func和Action是委托包装,Funcs返回一个值(名称可能来自VBs函数..?),而Actions不。

Like CSharpAtl said, since they are both wrappers, you could just as well make your own delate, with a name that makes more sense to you, (like VoidFunc(int val1 ,int val2)) , thought I think Func and Action are pretty standard. 就像CSharpAtl所说的那样,由于它们都是包装器,因此您也可以自己命名,使用对您更有意义的名称(例如VoidFunc(int val1 ,int val2)) ,以为我认为FuncAction很漂亮标准。

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

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