bsh152s
bsh152s

Reputation: 3218

What should be the standard for private class constants in C#?

For those of you that use the underscore prefix for private class members in C# (i.e. private int _count;), what do you use for private constants? I'm thinking the following but am curious as to what others are doing.

private const int _MaxCount;

Upvotes: 1

Views: 1125

Answers (4)

HuBeZa
HuBeZa

Reputation: 4761

I'm using:

private const int MAX_COUNT = 42;

I do not use PascalCasing because that's my standard for properties.
I do not use camelCasing because that's my standard for local variables.
I do not use _camelCasing because that's my standard for private fields.
I do not use _PascalCasing because IMO it's hard to distinguish it from _camelCasing.

Upvotes: 3

M4N
M4N

Reputation: 96571

Well, private is private, so chose the convention you like best. I personally use PascalCasing, e.g:

private const int SomeConstant = 42;

This is what MSDN has to say about it:

The naming guidelines for fields apply to static public and protected fields. You should not define public or protected instance fields:

  • Do use Pascal casing in field names.
  • Do name fields with nouns or noun phrases.
  • Do not use a prefix for field names. For example, do not use g_ or s_ to distinguish static versus non-static fields.

Upvotes: 5

Joel Coehoorn
Joel Coehoorn

Reputation: 415820

The naming guidelines are available here:
http://msdn.microsoft.com/en-us/library/ms229002.aspx

As Mehrdad said, prefixes are specifically discouraged.

That said, they are just guidelines more than hard rules. Personally, I use an '_' prefix, but only for private members that directly provide the backing store for a public property, and then the names will otherwise match exactly.

There's no specific guidance for constants, so the Capitalization Conventions rules probably still fit.

Upvotes: 0

Mehrdad Afshari
Mehrdad Afshari

Reputation: 421998

C# and .NET naming conventions discourage all prefixes (e.g. C, i, s_, g_, m_, _) except "I" for interface names and "T" for type parameters.

Upvotes: 2

Related Questions