C# Variable Naming

Bali C picture Bali C · Jul 21, 2011 · Viewed 34.6k times · Source

I was wondering what the best way of naming a variable is in C#? I know there are several different ways but I was just wondering why some people prefer one over the other?

I tend to use a lowercase letter representing the type (I think this is the Hungarian method?) but a lot of people don't like this, which is the best way?

Example:

string sMyString
string MyString
string myString
string mystring

Thanks

Answer

Oded picture Oded · Jul 21, 2011

The most common convention I have seen with c# when naming fields and local variables is camel case:

string myString

Here are the Microsoft guidelines for capitalization in c#. They state (amongst others):

The following guidelines provide the general rules for identifiers.

Do use Pascal casing for all public member, type, and namespace names consisting of multiple words.

Note that this rule does not apply to instance fields. For reasons that are detailed in the Member Design Guidelines, you should not use public instance fields.

Do use camel casing for parameter names.


I would add that this is something that needs to be agreed with your team members.