Reputation: 191
reading a book, it says that these two are obligate conventions:
But in many places then I can see, especially in properties, that author does not follow that, e.g.:
private int x
and
public int X
..
The same with underscores..sometimes he uses _x and X for properties.
What is the correct naming convention, please?
Upvotes: 4
Views: 3942
Reputation: 801
I would add that its common to use only capital letters for constants
public const int STATUS_OK = 0;
Upvotes: 0
Reputation: 273244
The rules are not the same for all identifiers. And there are multiple standards around, all are good as long as they are used consistently.
What I remember from the MS guidelines:
But camelCasing just means "don't start with a capital", field
, _field
, mField
and m_field
all qualify.
Upvotes: 8
Reputation: 660098
In addition to all the other fine answers I'll add that it is a bad practice to begin an identifier with two underbars. The C# specification says that we reserve the right to make things beginning with two underbars have special meanings. This is our escape hatch so that if we really need to, we can add new features to the language without breaking anyone. Or, we can use methods that begin with two underbars as special-purpose compiler generated methods.
Upvotes: 6
Reputation: 18567
It depends on taste or the team that you are working in.
variables never start with a capital letter or a number. You can use an underscore, which is preferred by many developers because all their field members are grouped together in visual studio by intellisense.
private int _number;
private int _Number;
Most of them prefer underscore and a capital letter because when writing a normal variable name you use camelCaseNotation.
You would write like this:
private int number;
but because you use an underscore the first char is an underscore and the next char would be uppercase (cf. camelCaseNotation) like this:
private int _Number;
For properties you just write them with a capital letter without prefixes:
public int Number { get; set; }
About parameters you can use the same underscore prefix as field members if you like that:
private int _Number;
public MyClass(int _Number) {
this._Number = _Number;
}
There is no right or wrong, just try to follow the language guidelines and adapt yourself to the team that you are working with. Discuss with your team what you will use and all use the same, which makes code much better to read.
About the m_ prefix notation or hungarian notation (sName, iNumber, ...) , this is a bit "old", try to avoid this (most C/C++-programmers use that which are new to C#).
Upvotes: 2
Reputation: 244752
As others have mentioned, naming conventions vary across developers and teams. The only really important thing is that you're consistent.
If you're looking for guidelines to follow, Microsoft publishes a set of General Naming Conventions for the .NET Framework. They're somewhat scattered around on their website, but this page is a good start.
The executive summary is as follows:
BackColor
)backColor
).Additionally, you will often see private variables (particularly those that have a corresponding public property) named with an underscore prefix (e.g. _backColor
). Another convention prepends an m_
.
Upvotes: 3
Reputation: 38825
I'm not entirely sure that there is a correct way of naming identifiers. Some people use underscores to denote member variables, some use a m_
prefix. Some use an underscore to denote variables that have been passed in as parameters.
As for capital letters, some use them some don't and some use "camel-casing" which is where the first letter is non-capital, but subsequent "words" are, e.g:
thisIsCamelCase
What does matter, is that you are consistent. If you decide to use _
as a prefix for members, make sure all members have that convention.
Personally, I use _
as a prefix for parameters and m_
as a prefix for members.
public class Vector
{
private float m_X;
private float m_Y;
private float m_Z;
public Vector(float _x, float _y, float _z)
{
m_X = _x;
m_Y = _y;
m_Z = _z;
} // eo ctor
} // eo class Vector
It definitely sounds dodgy that your book preaches this "never never never" then promptly ignores it's own advice.
Upvotes: 1