5

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#?

informatik01
  • 16,038
  • 10
  • 74
  • 104
merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • 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 Apr 04 '12 at 23:34
  • possible duplicate of [Pascal casing or Camel Casing for C# code?](http://stackoverflow.com/questions/149491/pascal-casing-or-camel-casing-for-c-sharp-code) – slugster Apr 04 '12 at 23:35
  • What to do when the parameter, field, property is all capital? E.g. SQL – Simon Apr 05 '12 at 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 May 27 '17 at 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 May 27 '17 at 11:16

4 Answers4

9

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)

n00dles
  • 255
  • 3
  • 18
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180
  • 12
    It does not mention conventions for local variables though. – merlin2011 Apr 04 '12 at 23:41
  • @merlin2011 I updated it to have the more rigorous version btw :) – Justin Pihony Apr 05 '12 at 05:19
  • 1
    **NOTE**: the link to Microsoft Conventions you provided is for the latest (as of now) [**.NET 4.5**](http://msdn.microsoft.com/en-us/library/ms229043.aspx), where there is NO discussion of instance fields. But the table you have posted here is taken from the [**.NET 4** version](http://msdn.microsoft.com/en-us/library/ms229043(v=vs.100).aspx), where there IS mention of instance fields. – informatik01 Aug 31 '13 at 21:31
  • 1
    @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 Sep 01 '13 at 02:43
  • What do you do if you have a property and a backing field? – toddmo Mar 03 '15 at 21:23
  • PascalCase the property and camelCase the field – Justin Pihony Mar 03 '15 at 22:36
  • This stuff seems to change by time and opinion. – n00dles May 26 '17 at 13:27
  • So `BindingSource BS = new BindingSource()` is the correct way? coz I've seen `bs` and `Bs` – n00dles May 26 '17 at 13:56
3

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

EDIT: this should be the new link, archived - Internal Coding Guidelines

NSGaga-mostly-inactive
  • 14,052
  • 3
  • 41
  • 51
  • 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 Mar 03 '15 at 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-mostly-inactive Mar 03 '15 at 22:30
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.

aKzenT
  • 7,775
  • 2
  • 36
  • 65
1

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.

Community
  • 1
  • 1
Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
  • 3
    Kirk, where does it say that a local variable or private field is treated the same as a parameter? – toddmo Mar 03 '15 at 21:17