naming convention: _property vs. this.property

private string _property;
public string Property
{
get { return _property; }
set { _property = value; }
}

private string property;
public string Property
{
get { return this.property; }
set { this.property = value; }
}

Is there a design guideline on how to name fields that are encapsulated by a property Wich of those two versions should be preferred and why In the 2nd version FXCop complains that Member names should never differ only by case. But most code I see seems to prefer the 2nd version ...




Answer this question

naming convention: _property vs. this.property

  • Zen210

    If you look at the guidelines for field usage, you see the usage of camelCasing for private fields and PascalCasing for public fields and properties.

    However, going through the .NET Framework documentation you will see numerous examples of the usage of "m_" and "_" prefixes. I used to have no real preference, but whenever I start a new project, I try to stick to the "m_" prefix for private fields, especially since I switch between VB.NET and C# every now and then. Using the camelCasing/PascalCasing combination in VB.NET for properties does not work.


  • OscarKwok

    It is not easy to answer your question because something like "best approach" does not exist at all. Anyway recommendation is to use:
    private string _field; for class fields that will be encapsulated by a property and
    private string instanceName; for local variables.

    Personally I'm using the second approach for both because the underscore indicates me some VB.NET approach :-)
    My recommendation is to use such approach which best fits to you.

    cheers


  • naming convention: _property vs. this.property