5

This is sort of a duplicate question, but there was no real resolution.

So here we go. Lets say I have:

public enum Color
{
    Red,
    Blue,
    Green
}

public class BlueUnicorn
{
    private const Color Color = Color.Blue;
}
  1. "Use a singular name for most Enum types, but use a plural name for Enum types that are bit fields." retreived from Microsoft's Enumeration Type Naming Guidelines Check!

  2. "Consider giving a property the same name as its type." retreived from Microsoft's Names of Type Members Check!

  3. I should be using a constant instead of readonly, according to Resharper enter image description hereCheck!

But I get a compiler error. It's odd though, no intellisense on this. The evaluation of the constant value for 'BlueUnicorn.Color' involves a circular definition.

I feel like I'm in a convention crack here. What should the field be named according to best practices naming convention?

Community
  • 1
  • 1
Levitikon
  • 7,749
  • 9
  • 56
  • 74
  • May I suggest renaming `Color` to `ColorChoice` for either the enum or the BlueUnicorn const? – Kane Apr 03 '13 at 13:18
  • @Kane: What's the rational for this? I don't think it fits. – Daniel Hilgarth Apr 03 '13 at 13:20
  • 1
    This is a bit off to the side -- Despite Resharper's advice, I would use readonly instead of const for any public that is not a natural constant. (like a day or something that _never_ changes). Reason: consts are copied and not referenced. So, if your code above is in ClassLibrary A, and exe B references A, and you build, the Color.Blue value is copied into exe B. That means that if you release a new version of the class lib that changes the const value to green, your exe will continue to use blue until you recompile it as well. – JMarsch Apr 03 '13 at 13:22
  • (although I suppose that you could argue that Color.Blue for a BlueUnicorn _is_ a natural constant :P – JMarsch Apr 03 '13 at 13:24
  • @JMarsch: His constant is private. – Daniel Hilgarth Apr 03 '13 at 13:24
  • Oh right -- yah, private const is ok. – JMarsch Apr 03 '13 at 13:25

2 Answers2

4

You can name that field just the way you do. To resolve the compilation error, simply add the namespace of the enum in front of Color.Blue.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
2

You are not in a convention crack (great term by the way!). Convention (2) does not apply as that is for properties, not fields or constants. Therefore, you can call it something like UnicornColor.

As a side note, I appreciate that this is probably not your real code (unless you genuinely are writing a Unicorn simulator!), but it seems to me that having a BlueUnicorn class is not right. Having a class that is defined based on a particular constant within that class seems too specific - surely Color would be an attribute of a Unicorn class?

I've no idea if this applies to your real code, but it might be your design is a bit wrong anyway, and the question never arises?

RB.
  • 36,301
  • 12
  • 91
  • 131