C#委托知识

委托:将一个方法作为参数 传递给另一个方法。

写法一:无返回值的委托

using System;

namespace 委托
{
    //声明一个委托
    public delegate void Delegate_Str(string[] array);

    class Program
    {
        static void Main(string[] args)
        {
            /*
             * 三个需求:
             * 1.将一个字符串数组中的每个元素都转换成大写;
             * 2.将一个字符串数组中的每个元素都转换成小写;
             * 3.将一个字符串数组中的每个元素两边都加上双引号;
             */
            string[] array_Str = { "abCDefG", "hiJKlmN", "OPqrSt", "UvWxYz" };
            //一般的方法调用
            //StrToUpper(array_Str);
            //StrToLower(array_Str);
            //StrSYH(array_Str);
            //委托调用
            //Test_Delegate(array_Str,StrToUpper);
            //Test_Delegate(array_Str, StrToLower);
            Test_Delegate(array_Str, StrSYH);
            for(int i=0;i<array_Str.Length;i++)
            {
                Console.WriteLine(array_Str[i]);
            }
        }

        public static void Test_Delegate(string[] array,Delegate_Str del)
        {
            //调用委托
            del(array);
        }

        public static void StrToUpper(string[] array)
        {
            for(int i=0;i< array.Length;i++)
            {
                array[i] = array[i].ToUpper();
            }
        }

        public static void StrToLower(string[] array)
        {
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = array[i].ToLower();
            }
        }

        public static void StrSYH(string[] array)
        {
            for(int i=0;i< array.Length;i++)
            {
                array[i] = "\"" + array[i] + "\"";
            }
        }
    }
}

写法二:有返回值的委托

using System;

namespace 委托
{
    //声明一个委托
    public delegate string Delegate_Str(string str);

    class Program
    {
        static void Main(string[] args)
        {
            string[] array_Str = { "abCDefG", "hiJKlmN", "OPqrSt", "UvWxYz" };
            //Str(array_Str, StrToUpper);
            //Str(array_Str, StrToLower);
            Str(array_Str, StrSYH);
            for(int i=0;i<array_Str.Length; i++)
            {
                Console.WriteLine(array_Str[i]);
            }
        }

        public static void Str(string[] array,Delegate_Str del)
        {
            for(int i=0;i<array.Length;i++)
            {
                array[i] = del(array[i]);
            }
        }

        public static string StrToUpper(string str)
        {
            return str.ToUpper();
        }

        public static string StrToLower(string str)
        {
            return str.ToLower();
        }

        public static string StrSYH(string str)
        {
            return "\"" + str + "\"";
        }
    }
}

写法三:匿名函数

using System;

namespace 委托
{
    //声明一个委托
    public delegate string Delegate_Str(string str);

    class Program
    {
        static void Main(string[] args)
        {
            string[] array_Str = { "abCDefG", "hiJKlmN", "OPqrSt", "UvWxYz" };
            //匿名函数写法
            //Str(array_Str, delegate (string str)
            // {
            //     return str.ToUpper();
            // });
            //Str(array_Str, delegate (string str)
            //{
            //    return str.ToLower();
            //});
            Str(array_Str, delegate (string str)
            {
                return "\"" + str + "\"";
            });
            for (int i=0;i<array_Str.Length; i++)
            {
                Console.WriteLine(array_Str[i]);
            }
        }

        public static void Str(string[] array,Delegate_Str del)
        {
            for(int i=0;i<array.Length;i++)
            {
                array[i] = del(array[i]);
            }
        }
    }
}

示例一:使用委托求数组的最大值

using System;

namespace 求数组的最大值
{
    //使用委托求数组的最大值
    public delegate int DelCompare(object o1, object o2);

    class Program
    {
        static void Main(string[] args)
        {
            object[] o ={ "abc","fdsfdsds","fdalfdalfdalfdfda","fdafdas"};
            //写法1:匿名方法
            //object result = GetMax(o, delegate (object o1, object o2)
            //   {
            //       string s1 = (string)o1;
            //       string s2 = (string)o2;
            //       return s1.Length - s2.Length;
            //   });

            //写法2:Lambda表达式
            object result = GetMax(o, (object o1, object o2) =>
               {
                   string s1 = (string)o1;
                   string s2 = (string)o2;
                   return s1.Length - s2.Length;
               });
            Console.WriteLine(result);
        }

        public static object GetMax(object[] nums,DelCompare del)
        {
            object max = nums[0];
            for(int i=0;i<nums.Length;i++)
            {
                if(del(max,nums[i])<0)
                {
                    max = nums[i];
                }
            }
            return max;
        }
    }
}

泛型委托:

using System;

namespace 泛型委托
{
    public delegate int DelCompare<T>(T t1, T t2);

    class Program
    {
        static void Main(string[] args)
        {
            //int[] nums = { 1, 2, 3, 4, 5 };
            //int max = GetMax<int>(nums, Compare);
            //Console.WriteLine(max);

            string[] names = { "abcd", "abcdefg", "abc", "zwyz" };
            string max = GetMax<string>(names, (string s1, string s2) =>
             {
                 return s1.Length - s2.Length;
             });
            Console.WriteLine(max);
        }

        public static T GetMax<T>(T[] nums,DelCompare<T> del)
        {
            T max = nums[0];
            for (int i = 0; i < nums.Length; i++)
            {
                if (del(max, nums[i]) < 0)
                {
                    max = nums[i];
                }
            }
            return max;
        }

        public static int Compare(int n1,int n2)
        {
            return n1 - n2;
        }
    }
}

Lambda表达式:

using System;
using System.Collections.Generic;

namespace Lambda表达式
{
    public delegate void DelOne();//无参无返回值
    public delegate void DelTwo(string name);//有参无返回值
    public delegate string DelThree(string name);//有参有返回值

    class Program
    {
        static void Main(string[] args)
        {
            DelOne del1 = () => { };//delegate() { };
            DelTwo del2 = (string name) => { };//delegate(string name) { }
            DelThree del3 = (string name)=> { return name; };//delegate(string name) { return name; };

            List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            list.RemoveAll(n => n > 4);
            foreach(var item in list)
            {
                Console.WriteLine(item);
            }
        }
    }
}

窗体传值:

using System;
using System.Windows.Forms;

namespace 窗体传值
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2(ShowMsg);
            frm2.Show();
        }

        void ShowMsg(string str)
        {
            label1.Text = str;
        }
    }
}

using System;
using System.Windows.Forms;

namespace 窗体传值
{
    //声明一个委托
    public delegate void DelTest(string str);

    public partial class Form2 : Form
    {
        public DelTest _del;

        public Form2(DelTest del)
        {
            this._del = del;
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _del(textBox1.Text);
        }
    }
}

运行结果为:

 多播委托:

using System;

namespace 多播委托
{
    public delegate void DelTest();

    class Program
    {
        static void Main(string[] args)
        {
            DelTest del = T1;
            del += T2;
            del += T3;
            del += T4;
            del -= T3;
            del();
        }

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

运行结果为:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林枫依依

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值