C#中这个算是什么
今天偶然看见个代码片段:
delegate int Mydg(int a,int b); //声明一个委托 public static class LambdaTest { public static int add(int a, int b) { return a + b; } public static int minus(int a, int b) { return a - b; } public static int oper(this int a, int b, Mydg mydg) { return mydg(a,b); } public static void Main() { Mydg md1 = new Mydg(add); Mydg md2 = new Mydg(minus); Console.WriteLine(1.oper(2,md1)); Console.WriteLine(3.oper(1,md2)); //下面使用lambda表达式 Console.WriteLine(1.oper(2, (a, b) => a + b)); Console.WriteLine(3.oper(1,(a,b)=>a-b)); } }
觉得和一个js片段类似,mark一下
require("sys"); function delegate(func, a, b) { return func(a, b);//天然的委托,或者说省略掉了委托,直接传递行为 } var operation = { add:function (a, b) { return a + b; }, minus:function (a, b) { return a - b; }, divi:function (a, b) { return a / b; }, mod:function (a, b) { return a % b; }, multi:function (a, b) { return a * b; } } var arr = []; arr[0] = delegate(operation.add, 1, 3); //直接定义 arr[0]=delegate(function(a,b){return a+b;},1,3); arr[1] = delegate(operation.minus, 1, 3) //arr[1] =delegate(function(a,b){return a-b;},1,3); arr[2] = delegate(operation.divi, 1, 3) arr[3] = delegate(operation.mod, 1, 3) arr[4] = delegate(operation.multi, 1, 3) for(var e in arr) console.log(arr[e])