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.
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.
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.
The guidelines are summarized here http://blogs.msdn.com/b/brada/archive/2005/01/26/361363.aspx and 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.
A good article to read on the development of C# style guidelines is here at StyleCop.
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(){...}
.