简体   繁体   English

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

[英]Naming Conventions in C# - underscores

I saw this at an MVC3 Razor tutorial at 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]我一直认为[文档]

In C#, I usually see it used only when defining the underlying private member variable for a public property.在 C# 中,我通常看到它仅在为公共属性定义底层私有成员变量时使用。 Other private member variables would not have an underscore.其他私有成员变量不会有下划线。 This usage has largely gone to the wayside with the advent of automatic properties though.但是,随着自动属性的出现,这种用法在很大程度上已经被淘汰了。

Or is it a new naming convention I am seeing?还是我看到的新命名约定? Very curious about that usage in Microsoft's own tutorial.对微软自己的教程中的这种用法非常好奇。

PS: The article is pretty good. PS:文章不错。 Its just that I tend to follow naming conventions for better readability.只是我倾向于遵循命名约定以获得更好的可读性。

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, eg _customerId . .NET的原始指导原则是永远不要使用下划线,除非它们是私有成员变量的一部分,然后仅作为前缀,例如_customerId This was probably inherited from MFC where 'm_' was used as a prefix for member variables. 这可能是从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.'. 私有成员变量和具有相同名称的参数之间的消歧应该使用'this。'来完成。 In fact all references to private members should be prefixed with '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(){...}

The guidelines are summarized here http://blogs.msdn.com/b/brada/archive/2005/01/26/361363.aspx and include the stipulation to use "this." 这些指南在http://blogs.msdn.com/b/brada/archive/2005/01/26/361363.aspx中进行了总结,并包含了使用“this”的规定。 instead of underscore. 而不是下划线。 But I find that peppering my code with "this."'s makes the code more wordy, cluttered and hard-to-read. 但是我发现用“this。”来代我的代码会使代码变得更加冗长,混乱和难以理解。 Furthermore it seems to be less often followed than underscore so as a convention, "_" seems more conventional. 此外,它似乎不像下划线那么频繁,因此作为惯例,“_”似乎更为传统。

In ASP. 在ASP中。 NET MVC 3 underscores are used more usually. NET MVC 3下划线更常用。 For example all your partial views you need to name with underscore like _MyPartialView . 例如,您需要使用下划线命名所有部分视图,例如_MyPartialView
It's going for easy distinguishing partial views and views in your application. 它可以轻松区分应用程序中的部分视图和视图。

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. 所以两种方式都是正确的,写下你感觉更舒服的方式。

With C# 7, we have a new use of underscore, indicating a "discarded" variable.在 C# 7 中,我们使用了下划线的新用法,表示“丢弃”变量。 This is common when using one of the TryParse methods -- we sometimes need either the return value or the out value, but not both.这在使用其中一种 TryParse 方法时很常见——我们有时需要返回值或输出值,但不是两者都需要。 In C# 7, if you use "_" as the variable name, the compiler will optimize the object code to never create the variable at all.在 C# 7 中,如果使用“_”作为变量名,编译器将优化 object 代码以根本不创建变量。 If you didn't need the output variable (you just wanted to know if the parse was successful) you might have a construction like this:如果您不需要 output 变量(您只是想知道解析是否成功),您可能会有这样的结构:

if (Int32.TryParse(input, out _)) 

Alternately, if you don't care about the parse success, only the output variable, you can assign that to _ like this:或者,如果您不关心解析成功,只关心 output 变量,您可以将其分配给 _ ,如下所示:

_ = Guid.TryParse(input, out Guid id);

The underscore by itself is a legal variable name, so you can start using this pattern right away.下划线本身就是一个合法的变量名,所以你可以马上开始使用这个模式。 Earlier versions of C# will just create a new variable with that name and not optimize the object code, but you will communicate to yourself and your fellow developers that the variable is unimportant to the function of your code.早期版本的 C# 只会创建一个具有该名称的新变量,而不是优化 object 代码,但您将与您自己和您的其他开发人员交流该变量对您的代码的 ZC1C425268E68385D1AB5074C17A94F1 不重要。

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

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