public class Car
{
// is event named correctly?
public event EventHandler<EventArgs> SomethingHasHappened;
private void MoveForward()
{
OnSomethingHasHappened();
}
// is the named correctly
protected virtual void OnSomethingHasHappened()
{
EventHandler<EventArgs> locum = SomethingHasHappened;
if(locum!= null)
{
locum(this, new EventArgs());
}
}
}
Well, the first point is that you define your own naming convention and there is no 'wrong' way to do it (as long as it's consistent).
Having said that, the Microsoft standards are good if your sharing your code with other.
Normally, you would have events names as:
public class Car
{
// is event named correctly?
public event EventHandler<EventArgs> SomethingHasHappened;
private void MoveForward()
{
OnSomethingHasHappened();
}
// is the named correctly
protected virtual void OnSomethingHasHappened()
{
EventHandler<EventArgs> locum = SomethingHasHappened;
if(locum!= null)
{
locum(this, new EventArgs());
}
}
}
Note that the event is titled without the 'On' prefix, and the event firing method is named with the 'On' prefix. The event firing method is also protected virtual so that derived classes can override to change/add to the behaviour as well as use it to fire the event themselves when required.
public event EventHandler SomethingHappened;
private void OnSomethingHappened()
{
SomethingHappened();
}
Then:
private void Car_SomethingHappened()
{
}
Not the cleanest code, but the naming is how I do it. If there isn't a clear local variable name or it doesn't make sense, I suffix the name with "Handler":
class Form{
public event EventHandler<EventArgs> MouseMove;
public virtual void OnMouseMove()
{
if(MouseMove != null)
{
MouseMove(this, new EventArgs());
}
}
}
class Application{
public Application()
{
Form form = new Form();
form.MouseMove += //Hook your own Method
}
}
I personally look at how Microsoft has named their events and how they name their handlers.
class Form{
public event EventHandler<EventArgs> MouseMove;
public virtual void OnMouseMove()
{
if(MouseMove != null)
{
MouseMove(this, new EventArgs());
}
}
}
class Application{
public Application()
{
Form form = new Form();
form.MouseMove += //Hook your own Method
}
}
I would say the naming convention is okay, but what i miss in you example WHAT happened?
So i would more specialize the name of the event itself (like MovedForward) or if you need it more generalized you should provide some additional information within the EventArgs about what has changed (like the ListChanged in BindingList).
发布评论
评论(5)
几乎
触发事件的方法 -
OnEvent
(来自RaiseSomethingHasHappened
)即
OnBeforeOpen
、OnClosing
、OnSomethigHasHappened
事件
Event
(来自OnSomethingHasHappened
),即
BeforeOpen
、Closing
,SomethingHasHappened
处理程序
<实例或有意义的名称><_>
(来自Car_SomethingHasHappened
)即
Form_BeforeOpen
、Window_Closing
、Car_SomethingHasHappened
->完美的Almost
The method to fire the event -
On<When>Event
(fromRaiseSomethingHasHappened
)i.e.
OnBeforeOpen
,OnClosing
,OnSomethigHasHappened
The event
<When>Event
(fromOnSomethingHasHappened
)i.e.
BeforeOpen
,Closing
,SomethingHasHappened
the handler
<The Instance or meaningful Name><_><Event>
(fromCar_SomethingHasHappened
)i.e.
Form_BeforeOpen
,Window_Closing
,Car_SomethingHasHappened
-> perfect嗯,第一点是您定义自己的命名约定,并且没有“错误”的方法来做到这一点(只要它是一致的)。
话虽如此,如果您与其他人共享代码,Microsoft 标准是很好的。
通常,事件名称如下:
请注意,事件的标题不带“On”前缀,事件触发方法使用“On”前缀命名。
事件触发方法也是受保护的虚拟方法,以便派生类可以重写以更改/添加行为,并在需要时使用它来触发事件本身。
Well, the first point is that you define your own naming convention and there is no 'wrong' way to do it (as long as it's consistent).
Having said that, the Microsoft standards are good if your sharing your code with other.
Normally, you would have events names as:
Note that the event is titled without the 'On' prefix, and the event firing method is named with the 'On' prefix.
The event firing method is also
protected virtual
so that derived classes can override to change/add to the behaviour as well as use it to fire the event themselves when required.我倾向于做相反的事情:
然后:
不是最干净的代码,但命名就是我的做法。如果没有明确的局部变量名称或者它没有意义,我会在名称后添加“Handler”:
I tend to do the opposite:
Then:
Not the cleanest code, but the naming is how I do it. If there isn't a clear local variable name or it doesn't make sense, I suffix the name with "Handler":
我个人研究了微软如何命名他们的事件以及他们如何命名他们的处理程序。
I personally look at how Microsoft has named their events and how they name their handlers.
我想说命名约定是好的,但是我在你的例子中错过了什么发生了什么?
因此,我会更专门化事件本身的名称(例如
MovedForward
),或者如果您需要更通用的名称,您应该在 EventArgs 中提供一些有关已更改内容的附加信息(例如BindingList
代码>)。
I would say the naming convention is okay, but what i miss in you example WHAT happened?
So i would more specialize the name of the event itself (like
MovedForward
) or if you need it more generalized you should provide some additional information within the EventArgs about what has changed (like theListChanged
inBindingList
).