C#中的多播委托和泛型委托

 更新时间:2022年05月04日 16:25:59   作者:農碼一生  
这篇文章介绍了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# 函数覆盖总结学习(推荐)

    C# 函数覆盖总结学习(推荐)

    下面小编就为大家带来一篇C# 函数覆盖总结学习(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-05-05
  • C#打印日志的方法总结

    C#打印日志的方法总结

    在本篇文章里小编给大家整理了关于C#如何打印日志的技巧总结,需要的朋友们跟着学习下。
    2019-03-03
  • c#文本加密程序代码示例

    c#文本加密程序代码示例

    这是一个加密软件,但只限于文本加密,加了窗口控件的滑动效果,详细看下面的代码
    2013-11-11
  • C#如何控制IIS动态添加删除网站详解

    C#如何控制IIS动态添加删除网站详解

    这篇文章主要给大家介绍了关于C#如何控制IIS动态添加删除网站的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用C#具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-11-11
  • 简介C#读取XML的两种方式

    简介C#读取XML的两种方式

    在程序中访问进而操作XML文件一般有两种模型,分别是使用DOM(文档对象模型)和流模型,使用DOM的好处在于它允许编辑和更新XML文档,可以随机访问文档中的数据,可以使用XPath查询
    2013-03-03
  • C#与SQL连接:GridView控件对数据库的操作

    C#与SQL连接:GridView控件对数据库的操作

    GridView控件操作方面的知识,需要的朋友可以参考一下
    2013-02-02
  • C#实现从windows剪贴板获取内容的方法

    C#实现从windows剪贴板获取内容的方法

    这篇文章主要介绍了C#实现从windows剪贴板获取内容的方法,涉及C#操作剪贴板的相关技巧,非常简单实用,需要的朋友可以参考下
    2015-05-05
  • 基于Avalonia实现自定义弹窗的示例详解

    基于Avalonia实现自定义弹窗的示例详解

    对于使用avalonia的时候某些功能需要到一些提示,比如异常或者成功都需要对用户进行提示,所以需要单独实现弹窗功能,并且可以自定义内部组件,这一期将手动实现一个简单的小弹窗,并且很容易自定义,希望大家喜欢
    2023-02-02
  • Unity3D基于陀螺仪实现VR相机功能

    Unity3D基于陀螺仪实现VR相机功能

    这篇文章主要为大家详细介绍了Unity3D基于陀螺仪实现VR相机功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-04-04
  • C#实现会移动的文字效果

    C#实现会移动的文字效果

    这篇文章主要为大家详细介绍了C#实现会移动的文字效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-04-04

最新评论