private void button1_Click(object sender, EventArgs e)
        {
            int Number = 200;         //设置Number的值为 200。
            Publishser pub = new Publishser();     //实例化Publishser
            Subscriber1 sub1 = new Subscriber1();     //实例化Subscriber1
            Subscriber2 sub2 = new Subscriber2();     //实例化Subscriber2
            Subscriber3 sub3=new Subscriber3();       //实例化Subscriber3
            pub.NumberChanged += sub1.OnNumberChanged;   //将方法绑定到委托
            pub.NumberChanged += sub2.OnNumberChanged;
            pub.NumberChanged += sub3.OnNumberChanged;
            pub.DoComething(Number);      //调用DoComething
        }    class Publishser
    {
        public delegate int DemoEventHandler(int num);      //定义委托,可代表的方法类型是整形
        public event DemoEventHandler NumberChanged;  //申明事件
        public void DoComething(int temp)
        {
            if (NumberChanged != null)
            {
                 temp=NumberChanged(temp);
            }
            MessageBox.Show(temp.ToString());
        }
    }    class Subscriber1
    {
        public int OnNumberChanged(int num)
        {
            MessageBox.Show("调用了Subscriber1类,num值为:"+ num);
            return num + 100; ;
        }
    }    class Subscriber2
    {
        public int OnNumberChanged(int num)
        {
            MessageBox.Show("调用了Subscriber2类 num值为:"+num);
            return num+100;
        }
    }    class Subscriber3
    {
        public int OnNumberChanged(int num)
        {
            MessageBox.Show("调用了Subcriber3类,num值为:"+num);
            return num+100;
        }
    } 我以为得到的结果是:                              调用了Subscriber1类,num值为:200                              调用了Subscriber2类,num值为:300                              调用了Subscriber3类,num值为:400                              500但是程序运行完之后结果却不是我所想的,结果是:                                                                    调用了Subscriber1类,num值为:200                                                                    调用了Subscriber2类,num值为:200                                                                    调用了Subscriber3类,num值为:200                                                                    300发现问题之后我用逐语句运行的方式调试程序,我发现当NumberChanged != null 第一次调用了Subscriber1之后并没有返回到Publishser类,也没有执行Subscriber1的 return,而是直接调用了Subscriber2,temp的值依旧是刚开始的200。执行完之后调用Subscriber3,只有当Subscriber3执行完了之后才返回到Publishser 这个时候temp的值等于300。为什么会这样?

解决方案 »

  1.   

    你并没有将返回值设定到Number上,被修改的Number只在方法内部。修改如下: class Program
        {
            static void Main(string[] args)
            {
                int Number = 200; //设置Number的值为 200。
                Publishser pub = new Publishser(); //实例化Publishser
                Subscriber1 sub1 = new Subscriber1(); //实例化Subscriber1
                Subscriber2 sub2 = new Subscriber2(); //实例化Subscriber2
                Subscriber3 sub3 = new Subscriber3(); //实例化Subscriber3
                pub.NumberChanged += sub1.OnNumberChanged; //将方法绑定到委托
                pub.NumberChanged += sub2.OnNumberChanged;
                pub.NumberChanged += sub3.OnNumberChanged;
                pub.DoComething(ref Number);            Console.Read();
            }
        }    class Publishser
        {
            public delegate void DemoEventHandler(ref int num); //定义委托,可代表的方法类型是整形
            public event DemoEventHandler NumberChanged; //申明事件
            public void DoComething(ref int temp)
            {
                if (NumberChanged != null)
                {
                    NumberChanged(ref temp);
                }
                Console.WriteLine(temp.ToString());
            }
        }    class Subscriber1
        {
            public void OnNumberChanged(ref int num)
            {
                Console.WriteLine("调用了Subscriber1类,num值为:" + num);
                num += 100;
            }
        }    class Subscriber2
        {
            public void OnNumberChanged(ref int num)
            {
                Console.WriteLine("调用了Subscriber2类 num值为:" + num);
                num += 100;
            }
        }    class Subscriber3
        {
            public void OnNumberChanged(ref int num)
            {
                Console.WriteLine("调用了Subcriber3类,num值为:" + num);
                num += 100;
            }
        }
      

  2.   

    当然是这样了的结果了,还能怎么样。至于原因不是你分析的那样“也没有执行Subscriber1的 return”这是错误的,每次执行都有执行return语句,但是你却没有想到那个“NumberChanged”是事件,并非普通方法。return是返回给“NumberChanged”事件,并不是返回给“Publishser”类,因此即使有返回值,“Publishser”类也是获取不到的,而“NumberChanged”事件要在所有内部方法执行完毕后才能给“Publishser”类返回值。