Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 738
thedrs Avatar asked May 27 '09 06:05

thedrs


People also ask

What should be the naming convention for methods?

Methods should be verbs in lowerCamelCase or a multi-word name that begins with a verb in lowercase; that is, with the first letter lowercase and the first letters of subsequent words in uppercase. Local variables, instance variables, and class variables are also written in lowerCamelCase .

How do you name a method in C#?

In C# the method name must start with uppercase letter and should be made of a verb or a couple: verb + noun. The name is formatted by following the Upper Camel Case convention (PascalCase), i.e. each word, including the first one, starts with uppercase. The brackets ( and ) always follow the name (without spaces).

What is naming convention in Visual Basic?

When you name an element in your Visual Basic application, the first character of that name must be an alphabetic character or an underscore. Note, however, that names beginning with an underscore are not compliant with the Language Independence and Language-Independent Components (CLS).


1 Answers

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)
{
like image 82
Lasse V. Karlsen Avatar answered Sep 21 '22 08:09

Lasse V. Karlsen