What is the correct casing convention for C# local and instance variables?

Go To StackoverFlow.com

1

There are a lot of former Java developers at my company who use camel casing in C# for both of these. What casing is most widely accepted / used for C#?

2012-04-04 23:30
by merlin2011
Get everyone a copy of ReSharper or equivalent (like JustCode from Telerik), it has rules built in for this sort of thing, you can quickly tell during code reviews whether something has been named inappropriately - slugster 2012-04-04 23:34
possible duplicate of Pascal casing or Camel Casing for C# code?slugster 2012-04-04 23:35
What to do when the parameter, field, property is all capital? E.g. SQ - Simon 2012-04-05 00:51
@slugster this is not a duplicate, as it does not asks for our opinion as the question you have referred to. This question is asking for the most widely accepted casing convention, which is objectively answerable if someone happens to have a statistic in his/her hands - Lajos Arpad 2017-05-27 07:45
@lago thanks for your comment on my comment which was made more than 5 years ago. The OP is asking for the de facto standard for casing. The linked possible duplicate answers exactly that. You should know by now how the site works - you can cast a reopen vote if you disagree with the closure reason (which wasn't for it being a duplicate). Are you going to contact the close voters and tell them they're wrong too - slugster 2017-05-27 11:16


7

Here is the Microsoft Conventions

enter image description here

Here is the full MSDN conventions

And here are the internal guidelines via Brad Abrams (covers just about everything, not just the highlights)

2012-04-04 23:33
by Justin Pihony
+1 for giving the source - Kendall Frey 2012-04-04 23:34
It does not mention conventions for local variables though - merlin2011 2012-04-04 23:41
@merlin2011 I updated it to have the more rigorous version btw : - Justin Pihony 2012-04-05 05:19
NOTE: the link to Microsoft Conventions you provided is for the latest (as of now) .NET 4.5, where there is NO discussion of instance fields. But the table you have posted here is taken from the .NET 4 version, where there IS mention of instance fields - informatik01 2013-08-31 21:31
@informatik01 Yah, the link points to the latest. I am going to leave it as your note addresses it, as well as the users can change to whatever version they want via the version dropdown on the page : - Justin Pihony 2013-09-01 02:43
What do you do if you have a property and a backing field - toddmo 2015-03-03 21:23
PascalCase the property and camelCase the fiel - Justin Pihony 2015-03-03 22:36
This stuff seems to change by time and opinion - n00dles 2017-05-26 13:27
So BindingSource BS = new BindingSource() is the correct way? coz I've seen bs and Bsn00dles 2017-05-26 13:56


2

The most common casing is camelCasing.

The Microsoft .NET Framework Reference Guidelines, require method parameters to be in camelCasing, since these are like local variables, I would treat them the same.

2012-04-04 23:33
by aKzenT


2

...and this one...
Internal Coding Guidelines (Design Guidelines, Managed code and the .NET Framework)
I like this one more, there is a clear-cut paragraph on naming/casing. It's only a tad more restrictive.

2012-04-04 23:46
by NSGaga
This is the only MS source that mentions local variables as per the OP's question. It's just a suggestion but still, if it's good enough for MS developers, I guess it's good enough for me - toddmo 2015-03-03 21:23
@toddmo there're exceptions to the rules, that's why some are suggestions. I for once couldn't live with the local variables as such (I really love mine with underscores:) I often forget 'this.' or find it annoying and then it could turn unreadable (mixed with locals) - and I know many people think the same. So I'd say find the guidelines you like the most, use that as a basis, change or adjust it slightly if need to - and stick with it (the most important thing of all - even more for the team) - NSGaga 2015-03-03 22:30


0

Exactly the same as in Java:

int someVeryLongLocalVariableName;

Official soure:

Camel Casing

The first letter of an identifier is lowercase and the first letter of each subsequent concatenated word is capitalized.

When an identifier consists of multiple words, do not use separators, such as underscores ("_") or hyphens ("-"), between words. Instead, use casing to indicate the beginning of each word.

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.

The following table summarizes the capitalization rules for identifiers and provides examples for the different types of identifiers.

2012-04-04 23:32
by Kirk Woll
Kirk, where does it say that a local variable or private field is treated the same as a parameter - toddmo 2015-03-03 21:17
Ads