Bali C
Bali C

Reputation: 31251

C# Variable Naming

What is the best way of naming a variable 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

Upvotes: 16

Views: 44177

Answers (6)

Alex R.
Alex R.

Reputation: 4754

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.

Upvotes: 3

Dulini Atapattu
Dulini Atapattu

Reputation: 2735

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

Upvotes: 0

Timur Sadykov
Timur Sadykov

Reputation: 11426

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 )

Upvotes: 0

J. Steen
J. Steen

Reputation: 15588

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.

Upvotes: 11

Oded
Oded

Reputation: 499362

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.

Upvotes: 32

Anders Abel
Anders Abel

Reputation: 69280

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.

Upvotes: 3

Related Questions