2012-02-03 345 views

回答

6

C#Eventsdelegates一种具体形式。如果你用其他语言编程,比如C++,你可以将delegate与一个函数(“方法”)指针进行比较 - 它指向内存中的一些代码。当你将指针作为一个方法调用时,你实际上是在指针指向的地址调用方法。

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

P.S.然而,与纯函数指针相比​​,它具有其他优势 - 您可以确定它会指向一个好看的方法,并采用正确数量和类型的参数并返回正确的类型。

3

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

+2

一个糟糕的问题的一个可怕的答案...即使我不了解你对事件的解释,并且我有*年*的C#经验! – 2012-02-03 10:18:10

+0

@Vladislav Zorov感谢您的反馈。你能否让我们都知道一个更好的方法来回答这样一个基本问题? – 2012-02-03 10:47:14

+1

希望我可以downvote Validislav佐罗夫的答案。也许只是英语不是你的东西?这是一个简单的解释。完成P.K. – sereschkin 2018-02-10 18:13:14

10

的方法是一个简单的类中包含实现一块的功能的代码。 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!"); 
    } 
} 
+0

通知的另一种方式是使用Observer模式。然后你可以在没有事件的情况下实现相同的代码为什么使用事件而不是观察者模式? – 2017-11-28 13:51:26

+0

@CodePope,因为这个答案是近6年前编码。编码范例随着时间而改变。 – adelphus 2017-12-01 18:25:57

+0

观察者模式远远早于六年。当时你写的是你的答案 – 2017-12-01 20:33:40

0

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

相关问题