Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming conventions for private members of .NET types [closed]

Normally when I have a private field inside a class or a struct, I use camelCasing, so it would be obvious that it's indeed private when you see the name of it, but in some of my colleagues' C# code, I see that they use m_ mostly or sometimes _, like there is some sort of convention.

Aren't .NET naming conventions prevent you from using underscores for member names?

And when you mention the MS naming conventions or what not, they tell you theirs is the best way, but don't explain the reasoning behind it.

Also when I am the owner of some code, where I clearly use camelCasing for private members, when they have to make a minor modification to the code, they stick in their conventions instead of following whatever conventions are there.

Is this a controversy?

like image 567
Joan Venge Avatar asked Mar 26 '10 20:03

Joan Venge


People also ask

How do you name a private variable in C#?

Use camel casing ("camelCasing") when naming private or internal fields, and prefix them with _ . When editing C# code that follows these naming conventions in an IDE that supports statement completion, typing _ will show all of the object-scoped members.

How do you name a private variable?

We have to start a variable name with a double underscore to represent it as a private variable (not really). Example:- one, two, etc..,. As we already said the variables whose names start with a double underscore are not private.


2 Answers

The .Net framework guidelines allow for a _ or m_ prefix on private field names because the provide no guidance on private fields. If you look at the BCL in reflector you'll notice a prefix is the most prevalent pattern.

The Reference page for naming fields is located here. Notice the guidelines only specify usage for public and protected fields. Private fields are simply not covered.

like image 139
JaredPar Avatar answered Sep 21 '22 15:09

JaredPar


Technically, underscores are a violation of .NET conventions (or at least used to be -- see comment thread), but Microsoft programmers themselves often use underscores, and many examples in the documentation use underscores. I think it's very helpful to be able to see at a glance which variables are member variables (fields) and which are local. The underscore really helps with this. It also nicely separates private member variables from local variables in intellisense.

Please see this very useful page for .NET naming conventions:

http://10rem.net/articles/net-naming-conventions-and-programming-standards---best-practices

And here's a page with Microsoft's official recommendations:

https://msdn.microsoft.com/en-us/library/ms229045%28v=vs.110%29.aspx

like image 28
devuxer Avatar answered Sep 18 '22 15:09

devuxer