首页 > 其他 > 详细

c# 关键字delegate、event(委托与事件)[MSDN原文摘录][2]

时间:2014-07-16 15:40:29      阅读:258      评论:0      收藏:0      [点我收藏+]
//Demo1:Declaring an event in an interface and implementing it in //a class.

// event_keyword.cs
using System;
public delegate void MyDelegate();   // delegate declaration

public interface I 
{
   event MyDelegate MyEvent;
   void FireAway();
}

public class MyClass: I 
{
   public event MyDelegate MyEvent;

   public void FireAway() 
   {
      if (MyEvent != null)
         MyEvent();
   }
}

public class MainClass 
{
   static private void f() 
   {
      Console.WriteLine("This is called when the event fires.");
   }

   static public void Main () 
   {
      I i = new MyClass();

      i.MyEvent += new MyDelegate(f);
      i.FireAway();
   }
}
//Demo2:One use for accessor-declarations is to expose a large //number of events without allocating a field for each event, but //instead using a hash table to store the event instances. This is //only useful if you have a very large number of events, but you //expect most of the events will not be implemented.

// event_keyword2.cs
using System;
using System.Collections;

public delegate void MyDelegate1(int i);
public delegate void MyDelegate2(string s);
public delegate void MyDelegate3(int i, object o);
public delegate void MyDelegate4();

public class PropertyEventsSample 
{
   private Hashtable eventTable = new Hashtable();

   public event MyDelegate1 Event1 
   {
      add 
      {
         eventTable["Event1"] = (MyDelegate1)eventTable["Event1"] + value;
      }
      remove
      {
         eventTable["Event1"] = (MyDelegate1)eventTable["Event1"] - value; 
      }
   }

   public event MyDelegate1 Event2 
   {
      add 
      {
         eventTable["Event2"] = (MyDelegate1)eventTable["Event2"] + value;
      }
      remove
      {
         eventTable["Event2"] = (MyDelegate1)eventTable["Event2"] - value; 
      }
   }

   public event MyDelegate2 Event3 
   {
      add 
      {
         eventTable["Event3"] = (MyDelegate2)eventTable["Event3"] + value;
      }
      remove
      {
         eventTable["Event3"] = (MyDelegate2)eventTable["Event3"] - value; 
      }
   }

   public event MyDelegate3 Event4 
   {
      add 
      {
         eventTable["Event4"] = (MyDelegate3)eventTable["Event4"] + value;
      }
      remove
      {
         eventTable["Event4"] = (MyDelegate3)eventTable["Event4"] - value; 
      }
   }

   public event MyDelegate3 Event5 
   {
      add 
      {
         eventTable["Event5"] = (MyDelegate3)eventTable["Event5"] + value;
      }
      remove
      {
         eventTable["Event5"] = (MyDelegate3)eventTable["Event5"] - value; 
      }
   }

   public event MyDelegate4 Event6 
   {
      add 
      {
         eventTable["Event6"] = (MyDelegate4)eventTable["Event6"] + value;
      }
      remove
      {
         eventTable["Event6"] = (MyDelegate4)eventTable["Event6"] - value; 
      }
   }
}

public class MyClass 
{
   public static void Main() 
   {
   }
}
//Deom3

public class SampleEventArgs
    {
        public SampleEventArgs(string s) { Text = s; }
        public String Text {get; private set;} // readonly
    }
    public class Publisher
    {
        // Declare the delegate (if using non-generic pattern).
        public delegate void SampleEventHandler(object sender, SampleEventArgs e);

        // Declare the event.
        public event SampleEventHandler SampleEvent;

        // Wrap the event in a protected virtual method
        // to enable derived classes to raise the event.
        protected virtual void RaiseSampleEvent()
        {
            // Raise the event by using the () operator.
            if (SampleEvent != null)
                SampleEvent(this, new SampleEventArgs("Hello"));
        }
    }

 

 

参考链接:

  http://msdn.microsoft.com/zh-cn/library/ms173171(v=vs.110).aspx

  http://msdn.microsoft.com/zh-cn/library/awbftdfh.aspx

  http://msdn.microsoft.com/zh-cn/library/8627sbea(v=vs.110).aspx

      

c# 关键字delegate、event(委托与事件)[MSDN原文摘录][2],布布扣,bubuko.com

c# 关键字delegate、event(委托与事件)[MSDN原文摘录][2]

原文:http://www.cnblogs.com/GYoungBean/p/3848128.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!