Naming guidelines with C#
-
18-09-2019 - |
Question
I have this class:
class DoSomething
{
private int timesDone;
...
}
Which is the right way to named variable 'timesDone'?
Sometimes I see named as m_timesDone. Is this correct? Where I can find information about naming guidelines?
Thank you!
Solution
According to MS standards your code is OK. Having prefixes as m_ is not really necessary when you have advanced IDE. However short prefix like _ can be used to take advantage of auto complete feature to quickly sort out class members.
I would recommend you to get a copy of "Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries" book to learn more on MS standards
OTHER TIPS
There is no universal right way. Chose a naming convention of your liking and stick with it.
Definitely do not use m_timesDone.
Simply put "private int timesDone".
You can learn about how to name variables by reading some good books such as Code Complete.
Many people do as you have it there. You would then reference it as
this.timesDone = someInt
However, I don't like this because I am not a fan of typing 'this' to avoid clashes with method parameter names. As long as it is readable and consistent you will be fine.
The convention of prefacing member fields with m_ comes from the early days of C++, when Hungarian notation was popular. It is not a C# convention, and since most C# code is written using a recent Visual Studio it adds visual noise without any corresponding advantage, because you can easily see the scope of a variable anyway. Do not use m_.
The lone example of Hungarian notation that has found its way into C# is the practice of prefacing interface class names with I, such as IDisposable.
You can find some information directly on the MSDN site: http://msdn.microsoft.com/en-us/library/ms229002.aspx
The only point of agreement that you will find is that it should not be TimesDown, but that it should start with a lower case letter.
In older publications (MS, MSDN), the use of a leading underscore is discouraged. Later it is back, especially for backingfields for properties: _timesDown.
The Strategy usually adopted is :
For Class & Method: Pascal Casing
e.g.
public class Program
{
}
e.g
public void DoSomething() { }
For Variables: Camel Casing e.g. timesDown
Local Variables:
aTimesDown
Global Variable:
myTimesDown
I hope this may help you :)