zoukankan      html  css  js  c++  java
  • C#的委托(delegate、Action、Func、predicate)

    委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递。事件是一种特殊的委托。

    1.委托的声明

    delegate我们常用到的一种声明

    delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型。

    namespace ConsoleApplication1
    {
        class Program
        {
            delegate void NumDelegate(int num);
            static void Main(string[] args)
            {
                AClass _a = new AClass();
                _a.cwdelegate = _a.AddNum;
                _a.cwdelegate(20);
                NumDelegate num20delegate = new NumDelegate(Add20);
                Console.WriteLine("-------------------------------------------");
                NumDelegate adddelegate = new NumDelegate(_a.AddNum);
                NumDelegate subdelegate = new NumDelegate(_a.SubNum);
                NumDelegate num30delegate = adddelegate + subdelegate;
                num30delegate(30);
                Console.WriteLine("-------------------------------------------");
                num30delegate += num20delegate;
                num30delegate(30);
                Console.WriteLine("-------------------------------------------");
                num30delegate += adddelegate;
                //去掉最后一个adddelegate
                num30delegate -= adddelegate;
                num30delegate(30);
                Console.ReadLine();
            }
            static void Add20(int num)
            {
                Console.WriteLine(string.Format("Add20: {0}", num));
            }
        }
    
        class AClass
        {
            public int result = 100;
            public delegate void CWDelegate(int num);
            public CWDelegate cwdelegate;
            public void AddNum(int num)
            {
                Console.WriteLine(string.Format("AddNum: {0}", num));
            }
    
            public void SubNum(int num)
            {
                Console.WriteLine(string.Format("SubNum: {0}", num));
            }
        }
    }

    2. Action

    Action是无返回值的泛型委托。

    Action 表示无参,无返回值的委托

    Action<int,string> 表示有传入参数int,string无返回值的委托

    Action<int,string,bool> 表示有传入参数int,string,bool无返回值的委托

    Action<int,int,int,int> 表示有传入4个int型参数,无返回值的委托

    Action至少0个参数,至多16个参数,无返回值。

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                AClass _a = new AClass();
                _a.cwdelegate = _a.AddNum;
                _a.cwdelegate(20);
    
                Console.WriteLine("-------------------------------------------");
                Action<int> act1 = _a.SubNum;
                Action<int> act2 = _a.AddNum;
                Action<int> act3 = act1 + act2;
                act3(30);
                Console.WriteLine("-------------------------------------------");
                act3 += _a.cwdelegate;
                act3(30);
                Console.WriteLine("-------------------------------------------");
                act3 -= _a.cwdelegate;
                act3(30);
                Console.ReadLine();
            }
            static void Add20(int num)
            {
                Console.WriteLine(string.Format("Add20: {0}", num));
            }
        }
    
        class AClass
        {
            public int result = 100;
            public Action<int> cwdelegate;
            public void AddNum(int num)
            {
                Console.WriteLine(string.Format("AddNum: {0}", num));
            }
    
            public void SubNum(int num)
            {
                Console.WriteLine(string.Format("SubNum: {0}", num));
            }
        }
    }

    3. Func

    Func是有返回值的泛型委托

    Func<int> 表示无参,返回值为int的委托

    Func<object,string,int> 表示传入参数为object, string 返回值为int的委托

    Func<object,string,int> 表示传入参数为object, string 返回值为int的委托

    Func<T1,T2,,T3,int> 表示传入参数为T1,T2,,T3(泛型)返回值为int的委托

    Func至少0个参数,至多16个参数,根据返回值泛型返回。必须有返回值,不可void

        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;
            }
        }

    4. predicate的使用

    泛型委托:表示定义一组条件并确定指定对象是否符合这些条件的方法。此委托由 Array 和 List 类的几种方法使用,用于在集合中搜索元素。

        class Program
        {
            static void Main(string[] args)
            {
                int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                var first = Array.Find(array, ProductGT10);
                Console.WriteLine("Found: X = {0}", first);
                Console.ReadKey();
            }
            private static bool ProductGT10(int x)
            {
                return x % 5 == 0;
            }
        }
  • 相关阅读:
    想要提高自己的写作水平?吃透这篇文章就够了
    Linux(Ubuntu)下搭建ASP.NET Core环境
    详解ASP.NET Core Docker部署
    《你有多少问题要请示》精华集粹
    5年,我从文员一路晋升到总监,薪资翻了5倍[转]
    《设计你的人生》的部分经典语录
    深入浅出Redis-redis哨兵集群[转]
    什么是全栈开发者
    Asp.net mvc中应用autofac
    js unicode处理
  • 原文地址:https://www.cnblogs.com/lgxlsm/p/4765248.html
Copyright © 2011-2022 走看看