Coder Perfect

Is there a naming standard for constants in C#?


private const int THE_ANSWER = 42;


private const int theAnswer = 42;

Personally, I believe that we should use camelCase with modern IDEs because ALL CAPS looks weird. What are your thoughts?

Asked by mmiika

Solution #1

For constants, use PascalCasing (Microsoft has a tool called StyleCop that describes all the desired standards and can check your source for compliance – albeit it is a little too anally retentive for many people’s liking). e.g.

private const int TheAnswer = 42;

Microsoft’s Framework Design Guidelines also document the Pascal capitalization convention.

Answered by Greg Beech

Solution #2

Upper Case is the way to go in terms of appearance. That way, it’s instantly recognizable. For the sake of uniqueness and leaving no chance for guessing, I vote for UPPER_CASE!

const int THE_ANSWER = 42;

Note: Upper Case is important when constants are used within the same file at the top of the page and for intellisense; but, if they are relocated to an independent class, using Upper Case makes little difference, as an example:

public static class Constant
    public static readonly int Cons1 = 1;
    public static readonly int coNs2 = 2;
    public static readonly int cOns3 = 3;
    public static readonly int CONS4 = 4;

// Call constants from anywhere
// Since the class has a unique and recognizable name, Upper Case might lose its charm
private void DoSomething(){
var getCons1 = Constant.Cons1;
var getCons2 = Constant.coNs2;
var getCons3 = Constant.cOns3;
var getCons4 = Constant.CONS4;

Answered by usefulBee

Solution #3

Actually, it is

private const int TheAnswer = 42;

At least, if you look at the.NET library, which is, in my opinion, the best method to decide on naming standards so that your code does not look out of place.

Answered by bh213

Solution #4

For const values, I still use uppercase, but it’s more out of habit than any special reason.

Of course, it’s easy to notice right away if anything is a problem. Do we truly need this information, in my opinion? Does it assist us in avoiding errors in any way? If I set a value to the const, the compiler will complain that I made a mistake.

My recommendation is to go with the camel casing. Maybe I’ll update my look as well;-)


That something smells hungarian is not really a valid argument, IMO. The question should always be: Does it help, or does it hurt?

There are times when knowing Hungarian comes in handy. There aren’t as many as there once were, but they still exist.

Answered by Treb

Solution #5

To begin, Hungarian Notation is the technique of displaying a parameter’s data type or intended purpose using a prefix. Hungarian Notation is not supported by Microsoft’s naming conventions notation

As stated here, using UPPERCASE is not recommended: SCREAMING CAPS and Pascal Case are acceptable conventions.

UPPERCASE can also be utilized if it is done to fit the existing scheme, according to Microsoft.

This pretty much sums up the situation.

Answered by user31939

Post is based on