简体   繁体   中英

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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