C# 中的命名约定 - 下划线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9782084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:50:37  来源:igfitidea点击:

Naming Conventions in C# - underscores

c#naming-conventions

提问by naveen

I saw this at an MVC3 Razor tutorialat http://www.asp.net

我在http://www.asp.net的 MVC3 Razor 教程中看到了这一点

public ActionResult Index() {

    return View(_usrs._usrList);

}

Isn't that usage plain wrong? I have always thought that [docs]

这种用法是不是完全错误?我一直认为[docs]

In C#, I usually see it used only when defining the underlying private member variable for a public property. Other private member variables would not have an underscore. This usage has largely gone to the wayside with the advent of automatic properties though.

在 C# 中,我通常看到它仅在为公共属性定义底层私有成员变量时使用。其他私有成员变量没有下划线。但是,随着自动属性的出现,这种用法在很大程度上已被搁置。

Or is it a new naming convention I am seeing? Very curious about that usage in Microsoft's own tutorial.

或者这是我看到的新命名约定?对微软自己的教程中的用法非常好奇。

P.S: The article is pretty good. Its just that I tend to follow naming conventions for better readability.

PS:文章还不错。只是我倾向于遵循命名约定以获得更好的可读性。

采纳答案by Phil

A good article to read on the development of C# style guidelines is here at StyleCop.

StyleCop上阅读了一篇关于 C# 样式指南开发的好文章。

The original guidance for .NET was to never use underscores unless they were part of a private member variable, and then only as a prefix, e.g. _customerId. This was probably inherited from MFC where 'm_' was used as a prefix for member variables.

.NET 的原始指南是永远不要使用下划线,除非它们是私有成员变量的一部分,然后仅作为前缀,例如_customerId. 这可能是从 MFC 继承的,其中“m_”用作成员变量的前缀。

Current practice is not to use underscores at all. Disambiguation between private member variables and parameters with the same name should done using 'this.'. In fact all references to private members should be prefixed with 'this.'.

目前的做法是根本不使用下划线。私有成员变量和同名参数之间的歧义应该使用“this.”来完成。事实上,所有对私有成员的引用都应该以“this.”为前缀。

The only place underscore seems to be used a lot is in unit test methods. I'm not a fan, but it may make the methods more readable, for example Throw_If_Customer_Is_Null(){...}.

唯一经常使用下划线的地方是单元测试方法。我不是粉丝,但它可能会使方法更具可读性,例如Throw_If_Customer_Is_Null(){...}

回答by Chuck Norris

In ASP. NET MVC 3 underscores are used more usually. For example all your partial views you need to name with underscore like _MyPartialView.
It's going for easy distinguishing partial views and views in your application.

在 ASP 中。NET MVC 3 下划线更常用。例如,您需要使用下划线命名的所有部分视图,如_MyPartialView.
它可以轻松区分应用程序中的部分视图和视图。

Anyway, in this example I don't prefer sing underscores, because there is no need to use them. It isn't wrong, because it's good practice to write with underline lists of your entities. But I will prefer to write without them.
So both ways are right, write in the way you feel more comfortable.

无论如何,在这个例子中我不喜欢唱下划线,因为没有必要使用它们。这没有错,因为使用实体的下划线列表编写是一种很好的做法。但我更愿意在没有它们的情况下写作。
所以两种方式都是对的,用你觉得更舒服的方式写。

回答by user316117

The guidelines are summarized here http://blogs.msdn.com/b/brada/archive/2005/01/26/361363.aspxand include the stipulation to use "this." instead of underscore. But I find that peppering my code with "this."'s makes the code more wordy, cluttered and hard-to-read. Furthermore it seems to be less often followed than underscore so as a convention, "_" seems more conventional.

这些准则在http://blogs.msdn.com/b/brada/archive/2005/01/26/361363.aspx 中进行了总结, 并包括使用“this”的规定。而不是下划线。但是我发现在我的代码中添加“this.”会使代码更加冗长、混乱和难以阅读。此外,它似乎比下划线更不常用,因此作为约定,“_”似乎更传统。