正确命名 C# 事件和处理程序

发布于 2024-09-11 18:59:03 字数 1431 浏览 6 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

太傻旳人生 2024-09-18 18:59:03

几乎

触发事件的方法 - OnEvent (来自 RaiseSomethingHasHappened

OnBeforeOpenOnClosingOnSomethigHasHappened

事件Event(来自OnSomethingHasHappened),

BeforeOpenClosing, SomethingHasHappened

处理程序<实例或有意义的名称><_>(来自 Car_SomethingHasHappened

Form_BeforeOpenWindow_ClosingCar_SomethingHasHappened ->完美的

Almost

The method to fire the event - On<When>Event (from RaiseSomethingHasHappened)

i.e. OnBeforeOpen, OnClosing, OnSomethigHasHappened

The event <When>Event (from OnSomethingHasHappened)

i.e. BeforeOpen, Closing, SomethingHasHappened

the handler <The Instance or meaningful Name><_><Event> (from Car_SomethingHasHappened)

i.e. Form_BeforeOpen, Window_Closing, Car_SomethingHasHappened -> perfect

雨后彩虹 2024-09-18 18:59:03

嗯,第一点是您定义自己的命名约定,并且没有“错误”的方法来做到这一点(只要它是一致的)。

话虽如此,如果您与其他人共享代码,Microsoft 标准是很好的。

通常,事件名称如下:

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()); 
  }
 }
}

请注意,事件的标题不带“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:

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.

在风中等你 2024-09-18 18:59:03

我倾向于做相反的事情:

public event EventHandler SomethingHappened;

private void OnSomethingHappened()
{
    SomethingHappened();
}

然后:

private void Car_SomethingHappened()
{

}

不是最干净的代码,但命名就是我的做法。如果没有明确的局部变量名称或者它没有意义,我会在名称后添加“Handler”:

private void SomethingHappenedHandler() {}

I tend to do the opposite:

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":

private void SomethingHappenedHandler() {}
冬天旳寂寞 2024-09-18 18:59:03

我个人研究了微软如何命名他们的事件以及他们如何命名他们的处理程序。

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
   }
}
一指流沙 2024-09-18 18:59:03

我想说命名​​约定是好的,但是我在你的例子中错过了什么发生了什么?

因此,我会更专门化事件本身的名称(例如 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 the ListChanged in BindingList).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文