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])
  • 相关阅读:
    C++中的friend函数详细解析(一)
    【图像处理算法】 直方图均衡化
    可降水量W:空中水文学名词初集(4)
    excel\docx
    WebService学习总结
    C#深入学习笔记Lock
    事件Event深入总结
    C#泛型委托与Lambda总结
    SQLServer事务与锁的基础概念总结
    委托Delegate深入总结
  • 原文地址:https://www.cnblogs.com/lansor/p/3285157.html
Copyright © 2011-2022 走看看