Hi
There is too much discussion about the class fields naming, but the major difference is this:
private int foo;
private int _foo;
Can anyone tell us, what's the latest decision about this convention?
Thank you!
Hi
There is too much discussion about the class fields naming, but the major difference is this:
private int foo;
private int _foo;
Can anyone tell us, what's the latest decision about this convention?
Thank you!
The latest decision according to "official" Field Usage Guidelines is:
private int foo;
Field names are written by convention in camelCase.
Also read Naming Guidelines - .NET Framework General Reference
The "usual" naming convention is to have the underscore for the private field and the same name without the underscore for the corresponding (if any) property.
I certainly won't start comparing myself to Jon Skeet, but here's my take on it.
You have two situations:
this..For each of these cases:
m_ and just _.In either case, be consistant, like with anything.
Here's what I've found:
"Recent" code in the .NET Framework has used both (though never in the same class, and generally the difference is across teams as a whole - WPF might use something different from Parallel FX (haven't checked if they do), but WPF blending effects would use the same as WPF text rendering).
StyleCop "suggests" using foo as a member field instead of _foo, and referencing it as this.foo inside of class members. The rationale there is:
this.memberName is clearer than memberName andthis.memberName, the _ doesn't do anything in regards to identifying the reference as a member field, because the this. already did that. Unndecessary redundancy clutters code, therefore no _ prefix.I personally, IMO prefer _foo for two reasons that form the rationale for the other method you've observed:
Personally I prefer _foo for a private field and Foo for a public field as follows
private string _foo;
public string Foo
{
get { return _foo; }
set { _foo = value; }
}
We follow this, which is a summary of MS .NET practices: http://www.irritatedvowel.com/Programming/Standards.aspx
Then it would be underscore for private fields.
private int _foo;