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

标签 c# naming-conventions

我在 an MVC3 Razor tutorial 看到了这个在 http://www.asp.net

public ActionResult Index() {

    return View(_usrs._usrList);

}

这种用法是不是完全错误?我一直认为 [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.

或者我看到的是新的命名约定?很好奇微软自己的教程中的那个用法。

P.S:文章写的不错。只是为了更好的可读性,我倾向于遵循命名约定。

最佳答案

一篇关于 C# 风格指南开发的好文章位于 C# coding conventions。 .

.NET 的最初指导方针是永远不要使用下划线,除非它们是私有(private)成员变量的一部分,然后只能作为前缀,例如_customerId。这可能是从 MFC 继承而来的,其中 'm_' 被用作成员变量的前缀。

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

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

关于c# - C# 中的命名约定 - 下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9782084/

相关文章:

c# - 如果文件已经以 Windows 方式存在,则自动重命名文件

.net - 参数命名: filename or fileName?

c# - 自定义属性的命名约定和指南

naming-conventions - PEP 8 : Function and method arguments naming convention

C#:无法撤消插入的文本

c# - .NET 扫描 API

c# - wpf如何自动聚焦于文本框

c# - 我这个翻译正确吗?

c# - 使用 MVVM 的 wpf 中的案例约定

django - Django View ,模型,表单,应用程序命名