我对Events
有些疑惑。 C#事件和方法之间的基本区别是什么?事件和方法之间的区别
回答
C#Events
是delegates
一种具体形式。如果你用其他语言编程,比如C++,你可以将delegate
与一个函数(“方法”)指针进行比较 - 它指向内存中的一些代码。当你将指针作为一个方法调用时,你实际上是在指针指向的地址调用方法。
这对于在调用者和被调用者之间提供解耦是必要的 - 所以当你发布调用它们的代码时,你不必拥有所有的方法(这是不可能的 - 表单控件开发者可以'当按下Button
时,可能知道需要调用的代码)。您可以调用指针,而其他开发人员稍后将其设置为适当的内存地址。
P.S.然而,与纯函数指针相比,它具有其他优势 - 您可以确定它会指向一个好看的方法,并采用正确数量和类型的参数并返回正确的类型。
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
的方法是一个简单的类中包含实现一块的功能的代码。 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!");
}
}
通知的另一种方式是使用Observer模式。然后你可以在没有事件的情况下实现相同的代码为什么使用事件而不是观察者模式? – 2017-11-28 13:51:26
@CodePope,因为这个答案是近6年前编码。编码范例随着时间而改变。 – adelphus 2017-12-01 18:25:57
观察者模式远远早于六年。当时你写的是你的答案 – 2017-12-01 20:33:40
.net中的事件是一对方法,一个用于“Add”,另一个用于“Remove”,每个方法接受一个委托。通常,“Add”方法将接受传入的委托并将其添加到委托的列表或MulticastDelegate;将委托传递给之前传递给“添加”方法的“删除”事件应从列表中删除该委托。如果没有其他要求,C#和vb.net默认会自动创建“添加”和“删除”事件,其行为如上所述。
- 1. jquery.simulate.js和jQuery事件方法之间的区别?
- 2. OnClick()事件和OnClickListener之间的区别?
- 3. JSF:ViewActions和preRender事件之间的区别
- 4. LinkLabel.Click和LinkLabel.LinkClicked事件之间的区别?
- 5. getRootNav()和navCtrl()方法之间的区别
- 6. “__method__”和“方法”之间的区别
- 7. PrintWriter.printf和PrintWriter.format方法之间的区别
- 8. EventLog.WriteEntry和EventLog.WriteEvent方法之间的区别
- 9. doGet和doHead方法之间的区别
- 10. Map.put和Map.putAll方法之间的区别?
- 11. GET和POST方法之间的区别?
- 12. $ http.get和方法之间的区别:'GET'
- 13. UIPickerView:didSelectRow方法和Value Changed事件处理程序方法之间的区别
- 14. Grails withTransaction()和事务服务方法之间的区别
- 15. OnLoad方法和Load事件的区别?
- 16. “DOMContent事件”和“加载事件”之间的区别
- 17. 事务和TransactionScope之间的区别
- 18. 事件与delegateEvents之间的区别?
- 19. Java:组件中的setPreferredSize()和setSize()方法之间的区别
- 20. 在c#中创建一个事件和匿名方法之间的区别#
- 21. 类方法和实例方法之间的区别?
- 22. 下划线js之间的区别_each方法和_.invoke方法
- 23. 跟进:类方法和实例方法之间的区别?
- 24. UdpClient.Send方法和UdpClient.Client.Send方法之间的区别
- 25. CA2000的级别变量和方法之间的区别警告
- 26. 双缓冲方法之间的区别
- 27. HTTP方法之间的区别
- 28. Laravel之间的区别路由方法
- 29. Javascript包装方法之间的区别
- 30. 单例方法之间的区别
一个糟糕的问题的一个可怕的答案...即使我不了解你对事件的解释,并且我有*年*的C#经验! – 2012-02-03 10:18:10
@Vladislav Zorov感谢您的反馈。你能否让我们都知道一个更好的方法来回答这样一个基本问题? – 2012-02-03 10:47:14
希望我可以downvote Validislav佐罗夫的答案。也许只是英语不是你的东西?这是一个简单的解释。完成P.K. – sereschkin 2018-02-10 18:13:14