C#中的命名约定-下划线 [英] Naming Conventions in C# - underscores

查看:906
本文介绍了C#中的命名约定-下划线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 MVC3 Razor教程" rel ="noreferrer"> http://www.asp.net

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

public ActionResult Index() {

    return View(_usrs._usrList);

}

这种用法不是很普通吗?我一直认为 [docs]

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

在C#中,我通常看到它仅在定义基础私有时使用 公共财产的成员变量.其他私人成员变量 不会有下划线.这种用法在很大程度上已经 自动属性的出现还遥遥无期.

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.

还是我正在看到的一种新的命名约定?很好奇微软自己的教程中的用法.

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

P.S:这篇文章很好.只是我倾向于遵循命名约定以提高可读性.

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

推荐答案

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

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.

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

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.'.

在单元测试方法中似乎经常使用的下划线是唯一的下划线.我不是迷,但是它可以使方法更易读,例如Throw_If_Customer_Is_Null(){...}.

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(){...}.

这篇关于C#中的命名约定-下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆