Have read through the MSDN naming guidelines and could not find a clear answer, other than that you should try to avoid underscores in general. Let’s say I have the following:
public class Employee { private string m_name; //to store property value called Name public string Name { get { return m_name; } set { m_name = value; } } public void ConvertNameToUpper() { //by convention should you use this return m_name.ToUpper(); //or this return Name.ToUpper(); } }
What is the proper naming convention for m_name in the above? For example, in code I inherit I commonly see:
- m_name
- _name
- name
- myName or some other random identifier
Which one (or another) is most commonly accepted?
As a follow-up, in the methods of the class, do you refer to the internal (private) identifier or to the public property accessor?
I think that, whatever naming convention you use, the most important thing is that you stay consistent. I mean, if you choose to name private members like _name , then always do it like this instead of once using _name, and the other time m_name. I personally use the underscore-prefix convention. (one of the reasons is because I use NHibernate, and NHibernate has a ‘field.camelcase-underscore’ access strategy.
For your other question: It depends on what you want to do.
Does your property contain extra logic, and do you want this logic to be executed when you refer it ? Then use the property. You don’t want to execute the logic ? Use the field. Eric Lippert has written a post regarding this on his weblog.
For your folluw-up: it all depends on the situation. If your property contains some additional logic, and you don’t want to execute that additional logic when accessed from within the class, then use the backing field …