29

What is the best practice, most commonly accepted naming conventions for private variables in C#?

  1. private int myInteger;
  2. private int MyInteger;
  3. private int mMyInteger;
  4. private int _myInteger;
  5. private int _MyInteger;
  6. Mysterious other option

Which do you use and why? (My company is fairly new to C# and I would like to pick the most "industry accepted" method to try and get into our coding standard.)

Vaccano
  • 4,077
  • 5
  • 34
  • 39

17 Answers17

45

The MSDN class design guidlines http://msdn.microsoft.com/en-us/library/ta31s3bc.aspx recommends option 1 - myInteger.

I have always used this style. I have a personal dislike for the _ character.

btlog
  • 626
28

I use option #4 above:

private int _myInteger;

I like to have some indication of scope in my variable names, and the underscore is sufficient for that purpose. It's also quite easy to read.

20

I use the following naming scheme:

  • 1st (myInteger) for local scoped variables
  • 2nd (MyInteger) for public properties
  • 4th (_myInteger) for private variables
Zafer
  • 599
  • 2
  • 10
15

I think the option 4 is really the most readable option. It helps you from having to do this:

public Person(string name, int age) 
{
    this.name = name;
    this.age = age;
}

It also makes all private members more noticeable. In the following example, where the heck is age coming from? Without the this qualifier it is harder to tell.

private void Method()
{
    var x = 2;
    var y = age + x;
}

This is way easier to understand:

private void Method()
{
    var x = 2;
    var y = _age + x;
}
ChaosPandion
  • 6,313
11

First off, PascalCasing is commonly reserved for public properties, consts, methods, etc of the class. So I would skip 2 and 5.

Second, hungarian notation is discouraged in the .NET world, so (uh, I think) 3 is right out. Assuming that's what is going on with 3.

That leaves with camelCasing and _camelCasing. I typically use _camelCasing for class variables, and plain old camelCasing for variables scoped to a method or narrower. Camel casing is the accepted standard used for method arguments, protected/private variable names and variables within a method or narrower scope.

I also like to prepend with the underscore so that my private variables are grouped in my intellisense. However, I only do this for variables scoped to a type. Variables declared within a method or narrower scope I leave the underscore off. Makes it easy to keep them separate and keep less used variables together.

Ripped Off
  • 3,757
4

private int integer

If you get confused between member and local variables in a method scope then you probably need to refactor.

2

I wouldn't call it "my" anything!

But I'd say

class C
{
     int VariableName { get; set; }
}

quite often this is nicer than having explicit variables. If I had an explicit private variable i'd call it int _variableName;

CJBrew
  • 121
2

I do option #4 because that's what the SSCLI looks like, but honestly I don't care that much on naming of private variable. Public is a different story.

BTW you forgot m_MyInteger

Conrad Frix
  • 4,165
  • 2
  • 20
  • 34
1

In C++ I tend to use _ as I switch editors a lot which doesn't allow me to see if it's private.

For C# I tend to to leave the _ away as Visual Studio allows me to see if it's private.

I tend to use the Camel Case way to do this.

1

I use 4 (private int _myInteger;) because:

private int myInteger;

This is how I name my local variables.

private int MyInteger;

This is how I name constants.

private int mMyInteger;

This is not C# style.

private int _MyInteger;

This looks strange.

1

I believe the best way to do it (in C#/.net anyway) is a combination of 2 and 6:

private int MyInteger { get; set; }

There's theoretically no variable at all here, but it looks and acts like a private instance variable. If we need to add some business logic to that value (it's a completely internal value, so we can do anything we want to it after all) then it's already 'propertyized' for us. A hot steaming cup of win!

Task
  • 437
  • 3
  • 7
1

with the underscore.

Bill Wagner explains why in Effective C#. But I would never name an integer myInteger, better something like _age or _length. Including the TypeName in the instance name is a horrible practice. Names should be self explanatory and since C# is Type-Safe types can be found out att all times.

1

You need to give a more specific example, but:

private int count, private int badFileCount, private static readonly int ReconnectAttemptsLimit

By the way, you get all this FREE when you install and start using the latest and greatest MSFT Stylecop.

Job
  • 6,459
0

I go by option 5: private int _MyFoo

I don't see any real competitive advantage over _myFoo though.

mafu
  • 313
0

Juval Lowy's IDesign C# Coding Standard is quite popular. This standard recommends prefixing private member variables with "m_" (Option 6). That's what we do in our team.

private int m_myInteger;

Option 4 (_myInteger) is an acceptable variation of this standard.

I didn't like the MSDN recommendation (myInteger), because it makes it difficult to tell a private member from a local variable. Their recommendation, of course, solves this problem by qualifying private members with this, which seems redundant to me.

azheglov
  • 7,185
0

Use camelCasing for private variables like myInteger

Consider a preceding _ if the variable is a backup for a property to reduce confusions-
Variable _myProperty for property MyProperty

Gulshan
  • 9,532
0

I have ReSharper name my variables, not only mine but everyone else does this as well. There is a great amount of consistency trough the project.

Sevki
  • 101