zoukankan      html  css  js  c++  java
  • C# 委托的学习

        delegate int GetCalculatedValueDelegate(int x, int y);    //定义是个委托实际上就是抽象一类  参数列表形式和返回值相同的函数AddCalculator,SubCalculator这两个该函数的参数形式和返回值的类型就是。
          
        static int AddCalculator(int x, int y)   
        {   
            return x + y;   
        }   
          
        static int SubCalculator(int x, int y)   
        {   
            return x - y;   
        }   
          
        static int Calculator(GetCalculatedValueDelegate del, int x, int y)   //定义一个函数 传入三个参数  第一个参数的类型是一个委托  也就是说委托是一个类型。
        {   
            return del(x, y);  
        }   
          
        static void Main(string[] args)   
        {   
            Console.WriteLine(Calculator(AddCalculator, 10, 20));   
        }   

    在上面的例子中,Calculator函数的第一个参数就是一个委托。事实上,Calculator对x和y将会做什么处理,它本身并不知道,如何处理x和y由GetCalculatedValueDelegate来决定。那么在Main方法里,我们将AddCalculator方法作为参数传递给Calculator,表示让Calculator用AddCalculator的逻辑去处理x和y。这也很形象:Calculator说:“我不知道要怎么处理x和y,让del去处理好了!”于是就把x和y扔给了del。

    lamada表达式的应用

    n=>n%2==0;

    等价于

    delegate(int n){ return n%2==0;}

  • 相关阅读:
    每天删除和创建表
    一种直接取数的建表语句
    正则替换函数regexp_replace()
    将同个id对应的多行数据,转为一行数组类型数据。使用collect_set() group by函数
    根据客户端标识加载不同的css文件
    配置npm镜像
    解决方案
    oracle virtualbox设置asm虚拟硬盘
    scala基础
    excel合并sheet内容
  • 原文地址:https://www.cnblogs.com/c-x-a/p/5959745.html
Copyright © 2011-2022 走看看