To sum it up, there are two basic trains of thought:
- The private field should just be CamelCase to match the .NET guidelines
- The private field should be CamelCase but with a _ pre-appended to tell the difference between method scope variables and class scope variables.
Original Post
Take the following example
public class Class1{
public string Prop1{
get {return m_Prop1;}
set {m_Prop1 = value; }
}
private string m_Prop1; // This is standard private property variable name.
// How do we cap this variable name? While the compiler can figure out same casing
// it makes it hard to read.
private Class2 Class2;
// We camel case the parameter.
public Class1(Class2 class2){
this.Class2 = class2;
}
}
Here are my stock rules
- The class name is capitalized (Class1)
- The public properties are capitalized (Prop1)
- The private field tied to a public property has
m_to indicate this. My coworker prefers_. There is some debate if usingm_or_should be used at all, as it is like Hungarian notation. - Private class fields are capitalized.
The part I am trying to figure out is what do I do if when the class name of a private field matches the private field name. For example, private Class2 Class2;. This is confusing.
If the private field name is not the same class, for example private string Name;, there isn’t much issue.
Or am I thinking about the issue in the wrong way? Should my classes and private fields be named in such a way that they don’t collide?
===
The consensus below is to use lower case private property names, but then we have this issue.
class1{
private string name; // This is scoped to the class.
public void Set(){
string firstName; // This is scoped to the method.
.... // Lot of code.
// Should use this.name but nothing prevents.
// Now there is confusion if name is a class property or scoped to the method.
name = firstName;
}
Or ReSharper’s guidelines.
Private properties, are cased like any other property.
Private fields, are lower case, starting with an underscore.