温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#中的多播委托和泛型委托实例分析

发布时间:2022-05-05 09:09:35 来源:亿速云 阅读:93 作者:zzz 栏目:开发技术

这篇“C#中的多播委托和泛型委托实例分析”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“C#中的多播委托和泛型委托实例分析”文章吧。

多播委托

简介

  • 每一个委托都是继承自MulticastDelegate,也就是每个都是多播委托。

  • 带返回值的多播委托只返回最后一个方法的值

  • 多播委托可以用加减号来操作方法的增加或者减少。

  • 给委托传递相同方法时 生成的委托实例也是相同的(也就是同一个委托)

代码实现

 	//声明委托
    delegate void MulticastTest();
    public class MulticastDelegateTest
    {
        
            
        public 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 = MethodTest;
            action += MethodTest2;
            action += MethodTest3;
            action -= MethodTest3;

            foreach (Action action1 in action.GetInvocationList())
            {
                action1();
            }
            Console.WriteLine("==========");
            action();
                        


            Func<string> func = () => { return "我是Lambda"; };
            func += () => { return "我是func1"; };
            func += () => { return "我是func2"; };
            func += GetTest;
            func += GetTest; //给委托传递相同方法时 生成的委托实例也是相同的(也就是同一个委托)
            
            string result = func();
            Console.WriteLine(result);
            Console.WriteLine("==========");
        }


        #region 委托方法
        public void MethodTest()
        {
            Console.WriteLine("我是方法MethodTest()1");
        }

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

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

        public string GetTest()
        {
            return "我是方法GetTest()";
        }
        #endregion
    }

泛型委托

代码实现

    //泛型委托声明
    delegate void GenericDelegate<T>(T t);
    public class GenericDelegate
    {
        public static void InvokeDelegate()
        {
            GenericDelegate<string> genericDelegate = new GenericDelegate<string>(Method1);
            genericDelegate("我是泛型委托1");

            //官方版本(不带返回值)
            Action<string> action = new Action<string>(Method1);
            action("我是泛型委托1");
            //Action<string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string>

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

            //官方版本(带回值)
            Func<string, string> func = new Func<string, string>(Method3);
            string ret = func("我是带返回值Func委托");
            Console.WriteLine( ret );
            //Func<string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string,string>
        }



        #region 委托方法

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

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

        public static string Method3(string str )
        {
            return str;
        }

        #endregion
    }

以上就是关于“C#中的多播委托和泛型委托实例分析”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI