C# Field Naming Guidelines?

Solution 1:

_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.

Solution 2:

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.

Solution 3:

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.

Solution 4:

Short answer: use _privateField, i.e. use leading underscore for private fields.

Long answer: here goes...

Long long ago, Microsoft used to suggest using camelCase for fields. See here. Note when that document was created, 10/22/2008. Pretty ancient.

Recent code base of Microsoft however depicts a different picture.

  1. Take a look at the C# Coding style of .NET Runtime GitHub repository. #3 is the point under discussion. Here is the relevant part

    We use _camelCase for internal and private fields and use readonly where possible.

  2. Also take a look at Coding style of Roslyn repository that specifically says that it follows the conventions of .NET Runtime.
  3. Take yet another look at the .NET Standard contributing page, which also says (at least for now) to follow the same guide as .NET CoreFX, which was a precursor to .NET Runtime.
  4. Prior to consolidation, CoreCLR also suggested following the same guide as CoreFX.
  5. Even WinForms repo speaks of using this same standard.
  6. I think I have said enough. So, to conclude, if you want to follow the guide that Microsoft suggests, I think you know what to do; use leading underscore for private fields like this: _privateField.

My opinion: I too personally prefer leading underscore for my private fields - makes it very easily distinguishable, without needing the this.