Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

share|improve this question
1  
C# has standards for this, see stackoverflow.com/questions/14967/… –  Tom Wijsman Sep 12 '10 at 14:01
5  
It's not nice to talk about private variables in public. Sorry, just had to. –  Mark C Oct 19 '10 at 14:30
5  
@Mark I think it should be "private members" for that to be suggestive. –  EpsilonVector Nov 1 '10 at 18:04
show 6 more comments

closed as not constructive by MichaelT, gnat, Martijn Pieters, Kilian Foth, ChrisF Apr 2 '13 at 21:18

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

17 Answers

up vote 39 down vote accepted

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.

share|improve this answer
1  
I have disliked the _ character till resharper added mid string matching to intellisence. Now I can type myInteger and it will match on _myInteger. But I did not know that MSDS says to not use the _ –  Vaccano Sep 9 '10 at 18:49
2  
If you use option 1, how can I tell if myInteger is a variable local to a method, or a private class member? –  Lorenzo Oct 19 '10 at 14:47
3  
@Lorenzo this.myInteger ;) –  TWith2Sugars Nov 1 '10 at 15:43
8  
But but... "this" is 4 characters and "_" is only one! Actually, that guideline makes a lot of sense but at my office everyone loves underscore and hates seeing "this.Foo", for whatever reason. Sometimes the only guidelines that matter are the ones your workplace forces on you. –  CodexArcanum Nov 1 '10 at 16:15
1  
The argument about the number of characters is debatable. You don't have to type this every time, only in the methods where a local variable with the same name is present. However, you are required to write an extra symbol every time if you use an underscore. With what I agree is that sticking to your local code style agreement is always important. –  Malcolm Feb 1 '12 at 23:30
show 4 more comments

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.

share|improve this answer
4  
I am not agree that it is easy to read, especially if you have to operate with several variables. –  Restuta Sep 29 '10 at 16:55
add comment

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;
}
share|improve this answer
1  
I used to swear by your first example, but after trying option #4 for a while I much prefer using underscores as a prefix for private fields. –  Jeremy Wiebe Sep 9 '10 at 17:47
2  
I say, use 1 for private variables, use 4 for private variables used in properties. –  Evan Plaice Sep 12 '10 at 1:22
show 1 more comment

I use the following naming scheme:

  • 1st (myInteger) for local scoped variables
  • 2nd (MyInteger) for public properties
  • 4th (_myInteger) for private variables
share|improve this answer
add comment

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.

share|improve this answer
1  
I don't understand why you would use _camelCasing for class variables, since its usually obvious that they are class variables. –  alternative Sep 9 '10 at 20:11
1  
@math no, its not obvious in the intellisense. Remember, fields (class-scoped variables) have (almost) the same icon as method scoped variables, so they look exactly the same if you don't look closely. The underscore helps differentiate them visually and keeps them grouped together, which helps if you aren't using them often (which you shouldn't be as state is the enemy of bug free programming). –  Will Sep 9 '10 at 20:24
2  
@mathepic: When Will says "class variables" he means (private) instance fields. You seem to have interpreted what he said to mean static members; but that's not what he said. –  Dan Tao Sep 20 '10 at 15:14
show 2 more comments

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!

share|improve this answer
add comment

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

share|improve this answer
add comment

private int integer

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

share|improve this answer
add comment

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.

share|improve this answer
1  
Yeah, it was an example though. –  Vaccano Sep 10 '10 at 0:32
add comment

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;

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

I go by option 5: private int _MyFoo

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

share|improve this answer
add comment

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.