简体   繁体   English

为什么visual studio打破了C#中方法的命名约定?

[英]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 (eg GetDeviceName). 我知道C#中类方法的命名约定是以大写字母开头,每个新单词都是大写字母(例如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 ? 所以我的问题是为什么当我创建一个表单时,在其上放置一个控件,然后双击该控件(对于由IDE自动为我创建的方法)我得到一个以非大写字母开头的方法? (eg s electButton_Click(object sender, EventArgs e) ) (例如s electButton_Click(object sender,EventArgs e))

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. 控件事件处理程序的命名约定一直是controlName_EventName ,所以基本上,它重用了你自己的控件命名约定,然后塞进事件的名称。

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. 这样做的结果是,像GhostDoc这样的工具可以识别这种格式,从而生成一些文档,虽然它们仍然是通用的,但它更像是试图自己推断方法的目的。

For instance, the "controlName_EventName" method could be documented like this: 例如,“controlName_EventName”方法可以这样记录:

/// <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): 而不是更像这样(因为GhostDoc处理上述问题,我根据对不良方法名称的经验在这里进行了解释):

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

It doesn't strictly break the .Net naming convention because the guidelines only apply to public methods. 它并没有严格违反.Net命名约定,因为该指南仅适用于公共方法。 However you could consider it breaking the spirit of the guidelines. 但是你可以认为它打破了准则的精神。

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. 我必须说我喜欢这种方式,因为它告诉我正在使用的控件的真实名称,而不是重命名的版本。

I feel you. 我感觉到你了。 Really, I kinda prefer the following naming convention: 真的,我更喜欢以下命名约定:

OnselectButtonClick()

Don't hit tab so quickly when wiring up said event handlers ;) Meh, it's all generated. 在连接所述事件处理程序时,不要这么快打到标签;)Meh,它都是生成的。 They could change the casing on it, but then the other half would cry about it. 他们可以改变它上面的外壳,但是另一半会为它而哭泣。 It's a lose - lose situation for them. 这对他们来说是一个双输的局面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM