thedrs
thedrs

Reputation: 1464

Why does visual studio break the naming convention for methods in C#?

I know the naming convention for class methods in C# is to begin with a capital letter and each new word is a capital letter (e.g. GetDeviceName).

So my question is why when I create a form, place a control on it, then double click the control (for the method to be created automatically for me by the IDE) I get a method begining with a non-capital letter ? (e.g. selectButton_Click(object sender, EventArgs e) )

Upvotes: 12

Views: 1221

Answers (5)

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391316

The naming convention for event handlers of controls have always been controlName_EventName, so basically, it reuses your own naming convention for the control, and then tucks on the name of the event.

This might be contrary to the general naming standard, but it has always been this way.

The upshot of this, is that tools like GhostDoc can recognize this format, and thus generate documentation that, while still generic, is more to the point, than if it were to try to deduce the purpose of the method by itself.

For instance, the "controlName_EventName" method could be documented like this:

/// <summary>
/// Handles the EventName event of the controlName control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance
/// containing the event data.</param>
protected void controlName_EventName(object sender, EventArgs e)
{

instead of more like this (since GhostDoc handles the above, I'm ad libbing here based on experience with bad method names):

/// <summary>
/// Control names the event name.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
protected void controlName_EventName(object sender, EventArgs e)
{

Upvotes: 12

Gromer
Gromer

Reputation: 9931

Don't hit tab so quickly when wiring up said event handlers ;) Meh, it's all generated. They could change the casing on it, but then the other half would cry about it. It's a lose - lose situation for them.

Upvotes: 1

Grokys
Grokys

Reputation: 16526

It doesn't strictly break the .Net naming convention because the guidelines only apply to public methods. However you could consider it breaking the spirit of the guidelines.

Upvotes: 4

Hao Wooi Lim
Hao Wooi Lim

Reputation: 3988

I feel you. Really, I kinda prefer the following naming convention:

OnselectButtonClick()

Upvotes: 0

CodeSpeaker
CodeSpeaker

Reputation: 850

Because in that case the name of your form control will be used since thats what its called. I must say i prefer this way since it tells me the real name of the control being used and not a renamed version.

Upvotes: 1

Related Questions