C#——委托

26 篇文章 1 订阅

1、委托初体验

namespace ConsoleApp1
{
    delegate void HelloDelegate(string msg);    //定义委托
    public class MainClass
    {
        ///1--委托初体验
        ///委托是一个引用类型,其实他是一个类型,保存方法的指针,他指向一个方法,
        ///当我们调用委托的时候这个方法就立即被执行
        public static void Main()
        {
            HelloDelegate helloDelegate = new HelloDelegate(Hello);//创建委托实例,参数为函数名
            // helloDelegate.Invoke("您好委托!");
            helloDelegate("您好委托!");//调用委托
            Console.ReadKey();
        }
       
        public static void Hello(string str)
        {
            Console.WriteLine(str);
        }
    }
    }

2、对于局部数据处理,实现分层处理数据

namespace ConsoleApp1
{
    delegate bool IsCsharpVip(LearnVip learnVip);//定义委托
    class MainClass
    {
        ///1--委托初体验
        ///委托是一个引用类型,其实他是一个类型,保存方法的指针,他指向一个方法,
        ///当我们调用委托的时候这个方法就立即被执行
        public static void Main()
        {
            List<LearnVip> learnVips = new List<LearnVip>();
            learnVips.Add(new LearnVip() { Id = 1, StudentName = "Ant1号", Price = 299 });
            learnVips.Add(new LearnVip() { Id = 1, StudentName = "Ant2号", Price = 399 });
            learnVips.Add(new LearnVip() { Id = 1, StudentName = "Ant3号", Price = 599 });
            learnVips.Add(new LearnVip() { Id = 1 , StudentName = "Ant4号", Price = 599 });
            IsCsharpVip isCsharpVip = new IsCsharpVip(GetVip);
            LearnVip.CsharpVip(learnVips, isCsharpVip);
            Console.ReadKey();
        }

        public static bool GetVip(LearnVip learnVip)
        {
            if (learnVip.Price == 599)
                return true;
            else
                return false;
        }

		public static bool GetVip2(LearnVip learnVip)
        {
            if (learnVip.Price == 5999)
                return true;
            else
                return false;
        }
    }

    class LearnVip
    {
        public int Id { get; set; }
        public string StudentName { get; set; }
        public int Price { get; set; }

        public static void CsharpVip(List<LearnVip>learnVips,IsCsharpVip isCsharpVip)
        {
            foreach(LearnVip learnVip in learnVips)
            {
                if(isCsharpVip(learnVip))
                    Console.WriteLine(learnVip.StudentName + "是VIP学员!");
            }
        }
    }
}

二、泛型委托

namespace CSharpAdvancedDelegate
{
    delegate void GenericDelegate<T>(T t);
    public class GenericDelegate
    {
        public static void InvokeDelegate()
        {
            //GenericDelegate<string> genericDelegate = new GenericDelegate<string>(Method1); //委托关联
            //genericDelegate.Invoke("我是泛型委托1");

            GenericDelegate<int> genericDelegate1 = new GenericDelegate<int>(Method2);
            genericDelegate1.Invoke(2);

            //官方版本(不带返回值,参数最多16个)
            Action<string> action = new Action<string>(Method1);
            action.Invoke("我是泛型委托1");

            //官方版本(带返回值,第一个参数为传入值,第二个为返回值)
            Func<string, int> func = new Func<string, int>(Method3);
            Console.WriteLine(func("我是带返回值的func委托"));
        }

        public static void Method1(string str)
        {
            Console.WriteLine(str);
        }

        public static void Method2(int i)
        {
            Console.WriteLine("我是委托2 "+i);
        }

        public static int Method3(string str)
        {
            int i = 100;
            return i;
        }
    }
}

在这里插入图片描述
在这里插入图片描述
三、多播委托

namespace CSharpAdvancedDelegate
{
    //delegate void MulticastTest();
    /// <summary>
    /// 多播委托
    /// 【1】每一个委托都是继承自MulticastDelegate,也就是每个都是多播委托
    /// 【2】带返回值的多播委托只返回最后一个方法的值
    /// 【3】多播委托可以用加减号来操作方法的增加或减少
    /// 【4】给委托传递相同方法时 生成的委托实例也是相同的(也就是同一委托)
    /// </summary>
    public static class MulticastDelegateTest
    {
        public static void Show()
        {
            //MulticastTest multicastTest = new MulticastTest(MethodTest);
            //multicastTest();

            //Action action = new Action(MethodTest);
            //action = (Action)MulticastDelegate.Combine(action, new Action(MethodTest2));
            //action = (Action)MulticastDelegate.Combine(action, new Action(MethodTest3));
            //action = (Action)MulticastDelegate.Remove(action, new Action(MethodTest3)); //减去方法
            //action();

            //简写
            Action action = MethodTest;
            action += MethodTest2;
            action += MethodTest3;
            action -= MethodTest3; //减去方法
            foreach (Action action1 in action.GetInvocationList())  //遍历委托中所有的方法
                action1();  //单独调用
           // action(); //执行委托中所有的方法


            Func<string> func = ()=> { return "我是Lambda"; };    //()表示无输入参数
            func += () => { return "我是Function1"; };
            func += () => { return "我是Function2"; };
            func += GetTest;
            func -= GetTest;
            Console.WriteLine( func()); //只返回最后一个结果
        }
        public static void MethodTest()
        {
            Console.WriteLine("我是方法MethodTest()");
        }

        public static void MethodTest2()
        {
            Console.WriteLine("我是方法MethodTest2()");
        }

        public static void MethodTest3()
        {
            Console.WriteLine("我是方法MethodTest3()");
        }

        public static string GetTest()
        {
            return "我是方法GetTest()";
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值