views:

371

answers:

5

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) )

+11  A: 

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)
{
Lasse V. Karlsen
didn't know event handlers had a different naming convention. thanks !
thedrs
+1  A: 

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.

CodeSpeaker
+1  A: 

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

OnselectButtonClick()
Hao Wooi Lim
+3  A: 

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.

Groky
+1  A: 

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.

Gromer