命名C#事件和妥善处理 [英] Naming C# events and handlers properly
问题描述
从我读过,我不知道如果我有事件和处理程序正确的命名约定。 (似乎有一些矛盾的意见在那里)。
From what I've read I'm not sure if I've got the naming convention for events and handlers correct. (there seems to be some conflicting advice out there).
在两类低于谁能告诉我,如果我有该事件的冠名权,引发事件以及处理该事件的方法的方法是什么?
In the two classes below can anyone tell me if I've got the naming right for the event, the method that raises the event and the method that handles the event?
public class Car
{
// is event named correctly?
public event EventHandler<EventArgs> OnSomethingHasHappened;
private void MoveForward()
{
RaiseSomethingHasHappened();
}
// is the named correctly
private void RaiseSomethingHasHappened()
{
if(OnSomethingHasHappened != null)
{
OnSomethingHasHappened(this, new EventArgs());
}
}
}
和用户等级:
public class Subscriber()
{
public Subscriber()
{
Car car = new Car();
car.OnSomethingHasHappened += Car_SomethingHasHappened();
}
// is this named correctly?
private void Car_SomethingHasHappened(object sender, EventArgs e)
{
// do stuff
}
}
在此先感谢!
推荐答案
几乎
触发事件的方法 - 针对<当>事件
(从 RaiseSomethingHasHappened
)
The method to fire the event - On<When>Event
(from RaiseSomethingHasHappened
)
即 OnBeforeOpen
, OnClosing
, OnSomethigHasHappened
事件<当>事件
(从 OnSomethingHasHappened
)
即 BeforeOpen
,结束
, SomethingHasHappened
处理程序<保证实例或有意义的名称>< _><事件>
(从 Car_SomethingHasHappened
)
即 Form_BeforeOpen
, WINDOW_CLOSING
, Car_SomethingHasHappened
- >完美
这篇关于命名C#事件和妥善处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!