C# Field Naming Guidelines?
_camelCase
for fields is common from what I've seen (it's what we use at our place and Microsoft prefer for the .NET Runtime).
My personal justification for using this standard is that is is easier to type _
to identify a private field than this.
For example:
void Foo(String a, String b)
{
_a = a;
_b = b;
}
Versus
void Foo(String a, String b)
{
this.a = a;
this.b = b;
}
I find the first much easier to type and it prevents me from ever accidentally assigning to the parameter called a
instead of this.a
.
This is reinforced by a Code Analysis Maintainability Rule that states:
- CA1500 Variable names should not match field names.
My other reason, is that this.
is optional (Visual Studio / Code prompts you to remove them) if it doesn't collide with a local variable or parameter name, making knowing which variable you are using harder. If you have an _
at the start of all private fields, then you always know which is a field and which is has local scope.
Follow the Microsoft Naming Guidelines. The guidelines for field usage indicate that it should be camelCase and not be prefixed. Note that the general rule is no prefix; the specific rule is not to prefix to distinguish between static and non-static fields.
Do not apply a prefix to field names or static field names. Specifically, do not apply a prefix to a field name to distinguish between static and nonstatic fields. For example, applying a g_ or s_ prefix is incorrect.
and (from General Naming Conventions)
Do not use underscores, hyphens, or any other nonalphanumeric characters.
EDIT: I will note that the docs are not specific with regard to private fields but indicate that protected fields should be camelCase only. I suppose you could infer from this that any convention for private fields is acceptable. Certainly public static fields differ from protected (they are capitalized). My personal opinion is that protected/private are not sufficiently different in scope to warrant a difference in naming convention, especially as all you seem to want to do is differentiate them from parameters. That is, if you follow the guidelines for protected fields, you'd have to treat them differently in this respect than private fields in order to distinguish them from parameters. I use this
when referring to class members within the class to make the distinction clear.
EDIT 2
I've adopted the convention used at my current job, which is to prefix private instance variables with an underscore and generally only expose protected instance variables as properties using PascalCase (typically autoproperties). It wasn't my personal preference but it's one that I've become comfortable with and probably will follow until something better comes along.
Generally there are two widely used ways to name fields (always using camelCase):
Using an underscore prefix
void F(String someValue) {
_someValue = someValue;
}
Using this.
to access the field and avoid name conflicts
void F(String someValue) {
this.someValue = someValue;
}
Personally I prefer the later, but I will use whatever convention is set forth by the organization I work for.