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
    }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • c#中的泛型委托详解

    今天学习一下c#中的泛型委托. 1.一般的委托,delegate,可以又传入参数(<=32),声明的方法为  public delegate void SomethingDelegate(int a); using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace delegateSummary { publ

  • C#基础之泛型委托实例教程

    本文实例讲述了C#中泛型委托的用法,并以示例形式较为详细的进行了用法分析.分享给大家供大家参考之用.具体如下: 首先,泛型委托是委托的一种特殊形式,虽然感觉看上去比较怪异,其实在使用的时候跟委托差不多,不过泛型委托更具有类型通用性. 就拿C#里最常见的委托EventHandler打比方.在.NET 2.0以前,也就是泛型出现以前,普通的事件处理函数都由EventHandler定义,如下: public delegate void EventHandler(object sender, Event

  • C#内置泛型委托之Action委托

    1.什么是Action泛型委托 Action<T>是.NET Framework内置的泛型委托,可以使用Action<T>委托以参数形式传递方法,而不用显示声明自定义的委托.封装的方法必须与此委托定义的方法签名相对应.也就是说,封装的方法必须具有一个通过值传递给它的参数,并且不能有返回值. 2.Action委托定义 查看Action的定义: using System.Runtime.CompilerServices; namespace System { // // 摘要: //

  • C#泛型委托的用法实例分析

    本文实例讲述了C#泛型委托的用法.分享给大家供大家参考.具体分析如下: 冒泡排序大家都知道,例如一个整形数组,可以用冒泡排序来使它按从小到大的顺序排序, 但它仅限于了按整形数组来排序,如何做到按任意类型进行排序呢,例如按一个类的某个属性进行排序? 举例说明:定义一组以类MEmployee为元素的数组,按MEmployee的Salary属性进行排序,类似的可以引伸为对其他类型的比较 元素类定义: public class MEmployee { public string Name { get;

  • C#中Predicate<T>与Func<T, bool>泛型委托的用法实例

    本文以实例形式分析了C#中Predicate<T>与Func<T, bool>泛型委托的用法,分享给大家供大家参考之用.具体如下: 先来看看下面的例子: static void Main(string[] args) { List<string> l = new List<string>(); l.Add("a"); l.Add("b"); l.Add("s"); l.Add("t&quo

  • 深入解析C#编程中泛型委托的使用

    在看泛型委托之前还需要先了解委托的概念. 这里讲的委托有两种类型一种是有返回值的,另一种是事件委托. //定义有返回值的委托 public delegate string GenricDelegate<T, S>(T title, S author); //定义事件委托. public delegate void GenricDelegateEnent<E,P>(E Name,P Address); public class GenericDelegateClass<V,F&

  • C#内置泛型委托之Func委托

    一.什么是Func委托 Func委托代表有返回类型的委托 二.Func委托定义 查看Func的定义: using System.Runtime.CompilerServices; namespace System { // // 摘要: // 封装一个方法,该方法具有两个参数,并返回由 TResult 参数指定的类型的值. // // 参数: // arg1: // 此委托封装的方法的第一个参数. // // arg2: // 此委托封装的方法的第二个参数. // // 类型参数: // T1:

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

    多播委托 简介 每一个委托都是继承自MulticastDelegate,也就是每个都是多播委托. 带返回值的多播委托只返回最后一个方法的值 多播委托可以用加减号来操作方法的增加或者减少. 给委托传递相同方法时 生成的委托实例也是相同的(也就是同一个委托) 代码实现 //声明委托 delegate void MulticastTest(); public class MulticastDelegateTest { public void Show() { MulticastTest multica

  • 使用.NET中的Action及Func泛型委托深入剖析

    委托,在C#编程中占有极其重要的地位,委托可以将函数封装到委托对象中,并且多个委托可以合并为一个委托,委托对象则可以像普通对象一样被存储.传递,之后在任何时刻进行调用,因此,C#中函数回调机制的实现基本上依赖于委托.C#的delegate关键字用于声明委托,它具有将声明委托类型映射到System.Delegate类的能力,System.Delegate类位于mscorlib.dll中,是.NET的基础核心类之一.使用delegate关键字声明一个委托,实质上创建了System.Delegate的

  • python3+PyQt5泛型委托详解

    自定义委托可以让我们对视图中出现的数据项的外观和行为进行完全控制.如果有很多模型,可能会希望不是全部的大多数模型能够仅用一个自定义委托,如果不能这么做,那么对于这些自定义委托,将很有可能存在大量重复代码.为了使得维护工作变得轻松,更好的方法为不要为每个模型创建一个自定义委托,而是用一系列的通用组件来共同构成一个委托.本文通过Python3+pyqt5实现了python Qt GUI 快速编程的16章的泛型委托例子. /home/yrd/eric_workspace/chap16/richtext

随机推荐