浅学C#(9)——多播、事件

版权声明:转载请注明出处 https://blog.csdn.net/le_17_4_6/article/details/86590635
多播

一次委托可以调用多个方法
通过+和-运算符可以实现多播的增加或减少


using System;

class SimpleClass
{
    public class WorkerClass
    {
        public int InstanceMethod(int nID, string sName)
        {
            int retval = 0;
            retval = nID * sName.Length;
            Console.WriteLine("调用InstanceMethod方法");
            return retval;
        }
        static public int StaticMethod(int nID, string sName)
        {
            int retval = 0;
            retval = nID * sName.Length+10;
            Console.WriteLine("调用StaticMethod方法");
            return retval;
        }
    }
    public delegate int SomeDelegate(int nID, string sName);
    static void Main()
    {
        //调用实例方法
        WorkerClass wr = new WorkerClass();
        SomeDelegate d1 = new SomeDelegate(wr.InstanceMethod);
        Console.WriteLine("Invoking delegate InstanceMethod, return={0}", d1(5, "aaa"));
        //调用静态方法
        SomeDelegate d2 = new SomeDelegate(WorkerClass.StaticMethod);
        Console.WriteLine("Invoking delegate StaticMethod, return={0}", d2(5, "aaa"));
        //多播
        SomeDelegate d3 = d1 + d2;
        Console.WriteLine("Invoking delegates d1 and d2(multi-cast),return={0} ", d3(5, "aaa"));
        //委托中的方法个数
        int num_method = d3.GetInvocationList().Length;
        Console.WriteLine("Number of methods referenced by delegate d3: {0}", num_method);
        //多播d3减去委托d2
        d3 = d3 - d2;
        Console.WriteLine("Invoking delegates d1 (multi-cast),return={0} ", d3(5, "aaa"));
        //委托中的方法个数
        num_method = d3.GetInvocationList().Length;
        Console.WriteLine("Number of methods referenced by delegate d3: {0}", num_method);

    }
}

将多个方法组合到一个委托中,如果方法的返回值非void,则通过委托调用方法仅得到最后一个方法的返回值,其它的返回值都将丢失
将多个方法组合到一个委托中,会顺序调用委托中的每个方法

using System;

class MathOperations
{
    public static double MultiplyByTwo(double value)
    {
        Console.WriteLine("in function MultiplyByTwo");
        return value * 2;
    }
    public static double Square(double value)
    {
        Console.WriteLine("in function Square");
        return value * value;
    }
    public static void MultiplyByTwo2(double value)
    {
        double result = value * 2;
        Console.WriteLine("Multiplying by 2: {0} gives {1}", value, result);
    }
    public static void Square2(double value)
    {
        double result = value * value;
        Console.WriteLine("Squaring: {0} gives {1}", value, result);
    }
}

delegate  double  DoubleOp(double x);
delegate  void  DoubleOp2(double x);

class Test
{
    static void Main()
    {
        DoubleOp operations = new DoubleOp(MathOperations.MultiplyByTwo);
        DoubleOp opr = new DoubleOp(MathOperations.Square);
        operations += opr;
        double result = operations(4.0);
        Console.WriteLine(result);
        operations -= opr;
        Console.WriteLine(operations(3.0));
        DoubleOp2 oprs = new DoubleOp2(MathOperations.MultiplyByTwo2);
        oprs += new DoubleOp2(MathOperations.Square2);
        oprs(5.0);
    }
}
事件

定义事件的委托
public delegate void EventHandler(object from, MyEventArgs e);
定义事件
public event EventHandler TextOut;
激活事件

if (TextOut !=null)
   TextOut(this, new EventArgs());

System.EventArgs是包含事件数据的类的基类
MyEventArgs类派生于EventArgs,实现自定
义事件数据的功能
订阅和取消订阅事件
evsrc.TextOut += new EventSource.EventHandler(CatchEvent);
evsrc.TextOut -= new EventSource.EventHandler(CatchEvent);

CatchEvent的签名要与定义的委托EventHandler的签名要相同

猜你喜欢

转载自blog.csdn.net/le_17_4_6/article/details/86590635