zoukankan      html  css  js  c++  java
  • 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])
  • 相关阅读:
    「PHP」使用 Homestead 作为 thinkphp5 的开发环境
    「PHP」Homestead 安装 swoole
    「PHP」Homestead
    存储过程
    Windows不能用鼠标双击运行jar文件怎么办?
    spring事务管理
    xml页面开头报错Multiple annotations found at this line
    修行
    jsp页面get和post不同导致的乱码问题
    VC执行Cmd命令,并获取结果
  • 原文地址:https://www.cnblogs.com/lansor/p/3285157.html
Copyright © 2011-2022 走看看