Naming local constants: UpperCamelCase or lowerCamelCase?

荒凉一梦 提交于 2019-12-01 04:55:29

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/3157431/naming-local-constants-uppercamelcase-or-lowercamelcase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!