Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming Conventions in C# - underscores

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.

like image 418
naveen Avatar asked Mar 20 '12 06:03

naveen


People also ask

Does C use Camelcase or Snakecase?

Classic C doesn't use camel-case; I've written code in camel-case in C, and it looks weird (so I don't do it like that any more). That said, it isn't wrong - and consistency is more important than which convention is used.

What is a name convention?

What Does Naming Convention Mean? Naming conventions are general rules applied when creating text scripts for software programming. They have many different purposes, such as adding clarity and uniformity to scripts, readability for third-party applications, and functionality in certain languages and applications.

What are the 3 rules for naming a variable?

A variable name must start with a letter or an underscore character (_) A variable name cannot start with a digit. A variable name can only contain alpha-numeric characters and underscores ( a-z, A-Z , 0-9 , and _ ) Variable names are case-sensitive (age, Age and AGE are three different variables)

What are the naming convention of identifier?

There are some rules you have to follow for naming identifiers: The first character of the identifier must be a letter of the alphabet (upper or lowercase) or an underscore ('_'). The rest of the identifier name can consist of letters (upper or lowercase), underscores ('_') or digits (0-9).


2 Answers

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

like image 189
Phil Avatar answered Oct 18 '22 19:10

Phil


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.

like image 35
user316117 Avatar answered Oct 18 '22 18:10

user316117