繁体   English   中英

事件和方法之间的区别

[英]Difference between Events and Methods

我对Events有些困惑。 C#事件和方法之间的基本区别是什么?

方法只是包含在类中的代码,以实现一个功能。 C#中的所有代码都包含在方法中。

至于事件,好吧,假设你有一个实现计数器的简单类(让我们称之为Counter对象)。 现在假设你想让其他与Counter无关的对象知道计数何时达到100.你会怎么做?

一种合乎逻辑的方法是允许其他对象在计数达到100时指定他们想要调用方法之一。然后,每个对象可以单独告诉Counter对象他们想要调用哪个方法。 Counter对象保存此方法列表,当计数达到100时,依次调用每个保存的方法。

这是事件如何工作-在Counter类包含一个事件成员(被称为说, CounterIs100 )的其他对象实例链接的自己的方法之一。 Counter对象检测到它已经达到100时,它会调用 CounterIs100成员,该成员自动调用当前链接到它的所有方法,从而依次通知每个对象计数确实达到了100。如果没有对象链接了CounterIs100事件成员的方法,它将为null ,因此Counter对象不需要调用事件成员。

class Counter
{
   // this is the count field used to save the current count value
   private int count;

   // this is the event member which holds all the methods other objects have specified
   public event CounterIs100Delegate CounterIs100;

   // This is a method. It invokes the CounterIs100 event member if anyone has subscribed to it
   protected void OnCounterIs100()
   {
       // see if anyone has subscribed (linked) their method to this event
       if (CounterIs100 != null)
       {
          // invoke the event - this will call all subscribed methods
          CounterIs100();
       }
   }

   // This is a method. It increments the counter variable stored by this object
   public void Increment()
   {
      count++;
      // if the count is 100, invoke the event
      if (count == 100)
         OnCounterIs100();
   }

}

// This is a delegate. It is used to define a template for other objects wishing to
// subscribe to the CounterIs100 event. The methods other objects link to the
// CounterIs100 event must match this declaration (although the name can be changed)
public delegate void CounterIs100Delegate();

// This is a class, unrelated to Counter, but uses its events
class SiteHits
{
     Counter hitCounter = new Counter();

     public SiteHits()
     {
         // We want to know when the number of site hits reaches 100.
         // We could monitor this ourselves, but we know the Counter class already
         // does this, so we just link our method to its event
         hitCounter.CounterIs100 += this.WhenSiteHitsReaches100;
     }

     public void PageRequested()
     {
         // someone has requested a page - increment the hit counter
         Console.WriteLine("We've been hit!");
         hitCounter.Increment();            
     }

     // this is the method we want called when the CounterIs100 event occurs.
     // note that the return value and parameters match CounterIs100Delegate above.
     public void WhenSiteHitsReaches100()
     {
         Console.WriteLine("Woohoo! We've reached 100 hits!");
     }
}

C# Events是一种特定形式的delegates 如果您使用其他语言(如C ++)进行编程,则可以将delegate与函数(“方法”)指针进行比较 - 它指向内存中的某些代码。 当您将指针作为方法调用时,实际上是在指针所指向的地址处调用该方法。

这对于在调用者和被调用者之间提供解耦是必要的 - 因此当您发布调用它们的代码时,您不必准备好所有方法(这是不可能的 - Forms控件开发人员不可能知道按下Button时需要调用的代码。 您调用指针,其他开发人员稍后将其设置为适当的内存地址。

然而,PS delegatesEvents具有优于普通函数指针的其他优点 - 您可以确定它将指向一个好看的方法,获取正确的参数数量和类型并返回正确的类型。

C#中的事件是一种类,当对象发生一些有趣的事情时,类可以向该类的客户端提供通知。

http://msdn.microsoft.com/en-us/library/aa645739%28v=vs.71%29.aspx

方法是包含一系列语句的代码块。 在C#中,每个执行的指令都是在方法的上下文中完成的。

http://msdn.microsoft.com/en-us/library/ms173114%28v=vs.80%29.aspx

.net中的事件是一对方法,一个用于“添加”,一个用于“删除”,每个方法都接受一个委托。 通常,“Add”方法将获取传入的委托并将其添加到委托列表或MulticastDelegate; 将委托传递给之前传递给“Add”方法的“Remove”事件应该从列表中删除该委托。 如果没有其他要求,C#和vb.net将默认自动创建“添加”和“删除”事件,其行为如上所示。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM