我能够编译包含以下内容的代码:
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/