c#委托的基本使用方法

来源:互联网 发布:美国和英国关系知乎 编辑:程序博客网 时间:2024/06/07 23:44
声明一个委托:
public delegate void DelSay(string name);
搭建第一个委托环境:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
 
namespace 委托
{
    //定义一个委托 必须跟要进行委托的函数签名一致(相同的参数、相同返回值)
    public delegate void DelSay(string name);
 
    class Program
    {
        static void Main(string[] args)
        {
            const string name = "张锋";
            /* 委托类似于将 函数指针 传入一个另一个函数中调用 */
            Test(name, Chiness);
            Test(name, English);
            Console.ReadKey();
        }
 
        /// <summary>
        /// 委托处理
        /// </summary>
        /// <param name="name"></param>
        /// <param name="d"></param>
        public static void Test(string name, DelSay d)
        {
            d(name);
        }
 
        public static void Chiness(string name)
        {
            Console.WriteLine("干嘛呢~ "+name);
        }
        public static void English(string name)
        {
            Console.WriteLine("hi "+name);
        }
    }
}

升级版委托环境:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
 
 
namespace 委托
{
    public delegate string DelPrint(string str);
    class Program
    {
        private static void Main(string[] args)
        {
            const string name = "张锋";
 
            string[] strArry = { "aaaaa""DDDDD" };
            /* 匿名函数的方式进行调用 */
            Console.WriteLine("小写");
            PrintfA(strArry, delegate(string str) { return str.ToLower(); });
 
            /* 通过Lambda 来进行调用 */
            Console.WriteLine("大写");
            PrintfA(strArry, str => str.ToUpper());
            Console.ReadKey();
        }
 
        /// <summary>
        /// 打印字符串
        /// </summary>
        /// <param name="str"></param>
        private static void PrintfA(string[] str, DelPrint del)
        {
            for (int i = 0; i < str.Length; i++)
            {
                str[i] = del(str[i]);
                Console.WriteLine(str[i]);
            }
             
        }
    }
}

泛型委托:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
 
namespace 泛型委托
{
    public delegate int DelMax<T>(T max,T temp);
    /// <summary>
    /// 通用取最大值
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            string[] str = {"a""bb""ccc""dddd"};
            int[] nums = {1, 2, 3, 4, 5, 6};
            string result = GetMax<string>(str, Test);
            int iresult = GetMax<int>(nums, Test);
            Console.WriteLine(result);
            Console.WriteLine(iresult);
            Console.ReadKey();
        }
 
        /// <summary>
        /// 通过泛型进行比较
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="tempObjects"></param>
        /// <param name="delMax"></param>
        /// <returns></returns>
        public static T GetMax<T>(T[] tempObjects, DelMax<T> delMax)
        {
            T max = tempObjects[0];
            for (int i = 0; i < tempObjects.Length; i++)
            {
                if (delMax(max, tempObjects[i]) < 0)
                {
                    max = tempObjects[i];
                }
            }
            return max;
        }
 
        public static int Test(int n1, int n2)
        {
            return n1 - n2;
        }
 
        public static int Test(string s1, string s2)
        {
            return s1.Length - s2.Length;
        }
    }
}

升级版泛型委托:
泛型委托时定义:public delegate int DelMax<T>(T max,T temp);
进行匿名函数或者lambda表示时需要的参数仅仅是针对调用的地方而言,跟泛型的定义没有关系,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
 
namespace 泛型委托
{
    public delegate int DelMax<T>(T max,T temp);
    /// <summary>
    /// 通用取最大值
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            string[] str = {"a""bb""ccc""dddd"};
            int[] nums = {1, 2, 3, 4, 5, 6};
 
            Console.WriteLine("1.0进行整数型比较");
            /*  整数比较大小 */
            //第一种 匿名函数
            int num = GetMax<int>(nums, delegate(int max, int temp) { return max - temp; });
            Console.WriteLine("匿名函数:" + num);
            //第二种 lambda 表达式
            num=GetMax<int>(nums, (int max, int temp)=> max - temp);
            Console.WriteLine("lambda:" + num);
 
            Console.WriteLine("2.0进行文本数型比较");
            /*  文本比较大小 */
            //第一种 匿名函数
            string strRt = GetMax<string>(str, delegate(string max, string temp) { return max.Length - temp.Length; });
            Console.WriteLine("匿名函数:" + strRt);
            //第二种 lambda 表达式
            strRt = GetMax<string>(str, (string max, string temp) => max.Length - temp.Length);
            Console.WriteLine("lambda:" + strRt);
             
            Console.ReadKey();
        }
 
        /// <summary>
        /// 通过泛型进行比较
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="tempObjects"></param>
        /// <param name="delMax"></param>
        /// <returns></returns>
        public static T GetMax<T>(T[] tempObjects, DelMax<T> delMax)
        {
            T max = tempObjects[0];
            for (int i = 0; i < tempObjects.Length; i++)
            {
                if (delMax(max, tempObjects[i]) < 0)
                {
                    max = tempObjects[i];
                }
            }
            return max;
        }
 
       
    }
}

多播委托:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
 
namespace 多播委托
{
    public delegate void SayHi();
    class Program
    {
        static void Main(string[] args)
        {
            SayHi hi = T1;
            hi += T2;
            hi += T3;
            hi += T4;
            hi();
            Console.WriteLine("-----------");
            hi -= T4;
            hi();
            Console.WriteLine("-----------");
            hi -= T3;
            hi();
            Console.WriteLine("-----------");
            Console.ReadKey();
        }
 
        public static void T1()
        {
            Console.WriteLine("我是T1函数");
        }
        public static void T2()
        {
            Console.WriteLine("我是T2函数");
        }
        public static void T3()
        {
            Console.WriteLine("我是T3函数");
        }
        public static void T4()
        {
            Console.WriteLine("我是T4函数");
        }
    }
}

0 0