zoukankan      html  css  js  c++  java
  • Func委托

    1. 无参有返回值

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                //1. 通过New关键字实例化,使用Invoke方法调用
                Func<int> fun1 = new Func<int>(FunWithNoPara);
                int result1 = fun1.Invoke();
                Console.WriteLine(result1);
    
                //2. 直接将方法赋值,当做普通方法方式调用
                Func<int> fun2 = FunWithNoPara;
                int result2 = fun2();
                Console.WriteLine(result2);
    
                //3. 使用匿名函数
                Func<int> fun3 = delegate() { return 10; };
                int result3 = fun3();
                Console.WriteLine(result3);
    
                //4. Lambda表达式(推荐)
                Func<int> fun4 = () => 10;
                int result4 = fun4();
                Console.WriteLine(result4);
                Console.ReadLine();
            }
    
            public static int FunWithNoPara()
            {
                return 10;
            }
        }
    }
    View Code

    2. 一个参数有返回值

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                //1. 通过New关键字实例化,使用Invoke方法调用
                Func<int, int> fun1 = new Func<int, int>(FunWithPara);
                int result1 = fun1.Invoke(1);
                Console.WriteLine(result1);
    
                //2. 直接将方法赋值给委托,当做普通方法调用
                Func<int, int> fun2 = new Func<int, int>(FunWithPara);
                int result2 = fun2.Invoke(1);
                Console.WriteLine(result2);
    
                //3. 使用匿名函数
                Func<int, int> fun3 = delegate(int p) { return p; };
                int result3 = fun3(1);
                Console.WriteLine(result3);
    
                //4. Lambda表达式
                Func<int, int> fun4 = p => p;
                int result4 = fun4(1);
                Console.WriteLine(result4);
                Console.ReadLine();
            }
    
            static int FunWithPara(int i)
            {
                return i;
            }
        }
    }
    View Code

    3. 多个参数有返回值

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication2
    {
        //定义委托
        class Program
        {
            static void Main(string[] args)
            {
                //1. 通过New关键字实例化,使用Invoke方法调用
                Func<int, string, bool> fun1 = new Func<int, string, bool>(FunWithMultiPara);
                bool result1 = fun1.Invoke(2, "abc");
                Console.WriteLine(result1);
    
                //2. 直接赋值,普通方法调用
                Func<int, string, bool> fun2 = FunWithMultiPara;
                bool result2 = fun1.Invoke(2, "2");
                Console.WriteLine(result1);
    
                //3. 使用匿名函数
                Func<int, string, bool> fun3 = delegate(int p, string q) { return p.ToString().Equals(q) ? true : false; };
                bool result3 = fun3(1, "1");
                Console.WriteLine(result2);
    
                //4. Lambda表达式
                Func<int, string, bool> fun4 = (p, q) =>
                {
                    return p.ToString().Equals(q) ? true : false;
                };
                bool result4 = fun4(1, "ed");
                Console.WriteLine(result3);
                Console.ReadLine();
            }
    
            static bool FunWithMultiPara(int i, string s)
            {
                return i.ToString().Equals(s) ? true : false;
            }
        }
    }
    View Code

     4. 委托当做方法参数

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine(Test<int, int>(Fun, 100, 200));
                Console.ReadKey();
            }
    
            public static int Test<T1, T2>(Func<T1, T2, int> func, T1 a, T2 b)
            {
                return func(a, b);
            }
            private static int Fun(int a, int b)
            {
                return a + b;
            }
        }
    }
    View Code
  • 相关阅读:
    红黑树(二)插入
    HDU 3415 Max Sum of Max-K-sub-sequence(单调队列)
    Codeforces 433 Div.2(A、B、C、D)
    Codeforces 846D Monitor(简单二分+二维BIT)
    hihoCoder 1403 后缀数组一·重复旋律(后缀数组+单调队列)
    CF 787D Legacy(线段树思想构图+最短路)
    HDU 6165 FFF at Valentine(Tarjan缩点+拓扑排序)
    Codeforces 165E Compatible Numbers(二进制+逆序枚举)
    Codeforces 672D Robin Hood(二分好题)
    HITOJ 2739 The Chinese Postman Problem(欧拉回路+最小费用流)
  • 原文地址:https://www.cnblogs.com/LuckyZLi/p/12888327.html
Copyright © 2011-2022 走看看