gpt4 book ai didi

C#;在不单独声明方法的情况下创建一个新委托(delegate)?

转载 作者:太空狗 更新时间:2023-10-29 20:59:58 25 4
gpt4 key购买 nike

我能够编译包含以下内容的代码:

OperationDelegate myOpDelegate;
static OperatorDefinition[] definitions ={
new OperatorDefinition("+",2,3,true, new OperationDelegate(OperationAdd)),
};
delegate double OperationDelegate(double[] args);
static double OperationAdd(double[] args)
{
return args[0] + args[1];
}

但我认为如果我能做更多像这样的事情,我的代码会看起来更干净:

OperationDelegate myOpDelegate;
static OperatorDefinition[] definitions ={new OperatorDefinition("+",2,3,true, new OperationDelegate({return args[0]+args[1]}))};
delegate double OperationDelegate(double[] args);

因为我想在一个地方定义每个 OperatorDefinition 的所有内容,而不是单独定义函数。在 C# 中有什么方法可以做到这一点吗?

(也欢迎对我的代码提出任何其他批评)

最佳答案

查看匿名方法...例如:C# - Anonymous delegate

关于C#;在不单独声明方法的情况下创建一个新委托(delegate)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3972209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com