17

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

Bali C
  • 30,582
  • 35
  • 123
  • 152

7 Answers7

33

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.

Community
  • 1
  • 1
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 3
    Strange that the list does not include local variables. They are different to parameters and properties. I would assume they intend variable names to match parameter names and use camel case. – Duncan Jones Feb 21 '13 at 07:50
12

The long-emerging trend is along the lines of

string meaningfulNameOfVariable;

with camel case, and clear names that are actually meaningful to you, your context, and to other developers.

J. Steen
  • 15,470
  • 15
  • 56
  • 63
4

I would go for string myString, that is the normal C# way. If you look at the samples in the MSDN documentation for .NET/C# you quickly get a feeling for the best practice.

Local variables are camelCased. Hungarian notation is considered bad practice in a type safe language. The type system takes care of knowing the type for you.

Anders Abel
  • 67,989
  • 17
  • 150
  • 217
  • 2
    Hungarian notation is generally only considered bad practice by those that don't understand Hungarian notation. It is **not** meant to denote the system type of the variable. – fearofawhackplanet Jul 21 '11 at 11:08
  • 1
    @fearofawhackplanet: You're right that if it is used as it was originally meant it is good. However hungarian notation has come to mean to denote the type. If it is used the right way, I agree that it is good, see e.g http://www.joelonsoftware.com/articles/Wrong.html for a good article on the subject. – Anders Abel Jul 21 '11 at 11:12
3

I would refer to the MSDN guidelines. My practice is, at least where I've worked, string myDescriptiveStringName for local variables and private string _myDescriptiveStringName for class variables. For properties, it would be public string MyDescriptiveStringName { get; set; }

But, most organizations follow (or at least they should) certain conventions. Better not deviate from those guidelines unless you want to be in the hot seat during peer reviews. ;-) There are a few exceptions to deviate, of course.

Alex R.
  • 4,664
  • 4
  • 30
  • 40
  • 1
    I have seen underscores used in projects however you shouldn't if sticking to the naming conventions "Do not use underscores, hyphens, or any other nonalphanumeric characters." [General Naming Conventions](http://msdn.microsoft.com/en-us/library/vstudio/ms229045(v=vs.100).aspx) – Paul C Sep 20 '13 at 11:39
0

In practice, this is the matter of agreement ) Naming becomes problem when everyone in the department writes by its own vision )

Use ReSharper or similar tools with same naming-convention properties within your department )

Timur Sadykov
  • 10,859
  • 7
  • 32
  • 45
0

It depends on different things... If you are coding on your own, you may use what you prefer... Normally I think most C# developers prefer camel case...

If you are working for a company etc. you have to adhere to their coding standards...

I think most important thing is that the variable names should be pretty much readable and that is the ultimate goal of better variable naming...

Dulini Atapattu
  • 2,735
  • 8
  • 33
  • 47