6

Which naming convention do you use for local constants in C# and why?

const int Pi = 3;
const int pi = 3;

It seems the trade-off is between lower camel-case indicating restricted scope, and upper camel-case being more readable and easier to move to a class level. I've noticed StyleCop prefers upper camel-case.

DavidRR
  • 18,291
  • 25
  • 109
  • 191
Anthony Faull
  • 17,549
  • 5
  • 55
  • 73
  • **Related:** [C# naming convention for constants?](http://stackoverflow.com/q/242534/1497596) – DavidRR Sep 13 '14 at 17:20
  • Microsoft refers to these naming conventions as **PascalCasing** and **camelCasing**. See the MSDN article [Capitalization Conventions](http://msdn.microsoft.com/en-us/library/ms229043.aspx). Also, please keep in mind that [pi](http://en.wikipedia.org/wiki/Pi) is arguably not a good example to use in this question since it is already defined in the .NET class library as [Math.PI](http://msdn.microsoft.com/en-us/library/ee435490.aspx). – DavidRR Sep 13 '14 at 17:28

2 Answers2

6

We use lower-case (camel casing) because local constants are almost local variables except of course you cannot modify them. (And we use camel casing for local variables of course...)

Daniel Brückner
  • 59,031
  • 16
  • 99
  • 143
2

I'm used to upper case (pascal case) for everything except variables and fields. Global constants are an exception to the fields, I don't know why, probably because they are public in some cases. Local constants are also lowercase so.

It's just a matter of taste imo. Of course, within a product / team, there should be an agreement.

On the other hand, our coding guideline requires full uppercase for constants, this would be PI in this case. I don't like this because upper cases are hard to read and need underlines for separation (which is against code analysis rules). Nobody follows this guideline anymore.

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193