微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

对于Action <>和/或Func <>,c#中的typedef等价

谷歌搜索后看起来并不乐观,但我想知道在使用Action< T>时是否存在某种混叠或类型定义方式.或者Func< in T,out TResult>在C#?

我已经看过Equivalent of typedef in c#了,它说在一个编译范围内你可以在某些情况下使用using构造,但就我所知,这似乎并不适用于Action和Func.

我想要这样做的原因是我想要一个动作用作多个函数的参数,如果我在某个时间点决定改变动作,那么很多地方都要改变它们作为参数类型和作为变量类型.

?typedef? MyAction  Action<int,int>;

public static SomeFunc(WindowClass window,int number,MyAction callbackAction) {
   ...
   SomeOtherFunc(callbackAction);
   ...
}

// In another file/class/...
private MyAction theCallback;

public static SomeOtherFunc(MyAction callbackAction) {
    theCallback = callbackAction;
}

是否有一些构造要使用,它可以定义代码段中指示的MyAction?

解决方法

在进行了一些搜索之后,似乎代表们正在拯救(见 Creating delegates manually vs using Action/Func delegatesA:
Custom delegate types vs Func and Action
).请评论为什么这不是解决方案或可能的陷阱.

有了代理,我可以重写给定代码示例的第一行:

public delegate void MyAction(int aNumber,int anotherNumber);
// Keep the rest of the code example

// To call one can still use anonymous actions/func/...
SomeFunc(myWindow,109,(int a,int b) => Console.Writeline);

原文地址:https://www.jb51.cc/csharp/100908.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐