A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 周琪 中级黑马   /  2013-5-18 20:11  /  1084 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

//给事件绑定和解除方法的方式不是只有+=和-=么?为什么我用只用=也可以呢。而在其他地方要用+=。

public delegate void MyDel();

    class Program
    {
        public static event MyDel MyEvent;
        static void Main(string[] args)
        {
            MyEvent = MyMethod;  //不用+=绑定
            DiaoYong();
            Console.ReadKey();
        }
        static void DiaoYong()
        {
            MyEvent();
        }
        static void MyMethod()
        {
            Console.WriteLine("同一个世界,同一个梦想。。。");
        }
    }

评分

参与人数 1技术分 +1 收起 理由
苏波 + 1

查看全部评分

1 个回复

倒序浏览
1 .作为委托参数传递的方法必须与委托声明具有相同的签名。
2. 例如:
// Declare a delegate
delegate void Del(int i, double j);
class MathClass
{
    staticvoid Main()
    {
       MathClass m = new MathClass();
        // Delegate instantiation using"MultiplyNumbers"
       Del d = m.MultiplyNumbers;
        //Invoke the delegate object.
       System.Console.WriteLine("Invoking the delegate using'MultiplyNumbers':");
       for (int i = 1; i <= 5; i++)
        {
           d(i, 2);
        }
    }
    //Declare the associated method.
    voidMultiplyNumbers(int m, double n)
    {
       System.Console.Write(m * n + " ");
    }
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
输出
Invoking the delegate using 'MultiplyNumbers':
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马