Kenoyer130
Kenoyer130

Reputation: 7308

Code casing question for private class fields

To sum it up, there are two basic trains of thought:

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 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;
}

Upvotes: 1

Views: 6466

Answers (6)

Ikaso
Ikaso

Reputation: 2268

I think that your issue would be solved if you stick to the coding conventions of the .NET Framework. For example, private members start with lower case.

Upvotes: 3

Janko R
Janko R

Reputation: 137

As mentioned before, you should follow the Framework Desing Guidelines. But nevertheless I'm curious why you differ in your naming convention between "private field tied to a public property" and "private class fields"


concerning the updated version of your question: if your method is so long that you are not able to distinguish if a variable was decleared in the method body or as a paramter, than you should think about refractoring.... This is the best answer to this problem that I have heard so far, it is not the best one though.

Upvotes: 1

Klaus Byskov Pedersen
Klaus Byskov Pedersen

Reputation: 120937

You should just follow Microsoft's naming guidelines.

And remember to run code-analysis to make sure that you have done it right.

Upvotes: 11

Cornelius
Cornelius

Reputation: 3616

I would say don't use m_ or _ to prefix private fields and name your fields better, if it is named better it would not collide by default.

Upvotes: 4

CaffGeek
CaffGeek

Reputation: 22054

Or ReSharper's guidelines.

Private properties, are cased like any other property. Private fields, are lower case, starting with an underscore.

    private string _foo = string.Empty;
    private string Bar { get; set; }

Upvotes: 8

Adam Lerman
Adam Lerman

Reputation: 3409

They should be named in a way that they do not collide, IMHO

Upvotes: 0

Related Questions