C# – Naming Conventions for Underscores

c++naming-conventions

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

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. 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: The article is pretty good. Its just that I tend to follow naming conventions for better readability.

Best Answer

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

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.

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

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