zoukankan      html  css  js  c++  java
  • 泛型(Generic)委托

    泛型委托我们之前已经用过很多了,泛型委托经常和lamuda表达式一起使用组成LINQ查询
    Action<>无返回值

        class Program
        {
            static void Main(string[] args)
            {
                Action<string> action1 = new Action<string>(Say);
                action1.Invoke("Abe");
                Action<int> action2 = Mui;
                action2(1);
            }
    
            static void Say(string str)
            {
                Console.WriteLine($"Hello,{str}!");
            }
            static void Mui(int x)
            {
                Console.WriteLine(x * 100);
            }
        }
    

    Func<>有返回值

        class Program
        {
            static void Main(string[] args)
            {
                Func<int, int, int> func1 = new Func<int, int, int>(Add);
                Func<double, double, double> func2 = new Func<double, double, double>(Add);
                var result1 = func1(100, 100);
                var result2 = func2(100.1, 100.2);
                Console.WriteLine(result1);
                Console.WriteLine(result2);
            }
    
            static int Add(int a, int b)
            {
                return a + b;
            }
    
            static double Add(double a, double b)
            {
                return a + b;
            }
        }
    


    z
    lambda表达式与泛型委托的结合:

        class Program
        {
            static void Main(string[] args)
            {
                Func<int, int, int> func1 = new Func<int, int, int>((a, b) => {return a + b; });
                Func<double, double, double> func2 = new Func<double, double, double>((a, b) => { return a + b; });
                var result1 = func1(100, 100);
                var result2 = func2(100.1, 100.2);
                Console.WriteLine(result1);
                Console.WriteLine(result2);
            }
        }
    

    把函数

            static int Add(int a, int b)
            {
                return a + b;
            }
    

    (a, b) => {return a + b; }
    


    表示,a,b的类型呢是由泛型Func<参数类型,参数类型,返回值类型>来限定

  • 相关阅读:
    [转]Linux里的2>&1究竟是什么
    一段shell脚本分析
    [整理]Linux Crontab命令总结
    random seed()函数
    clear命令新认识
    泛型与发射初探,获取当前代码所在的行
    tomcat集群(转)
    查看本地电脑的端口及对应的使用程序
    信息摘要算法小试牛刀
    Linux非root用户安装jdk和tomcat(转)
  • 原文地址:https://www.cnblogs.com/maomaodesu/p/11653865.html
Copyright © 2011-2022 走看看