Reputation: 42394
The guidelines are clear for enumerations...
Do use a singular name for an enumeration, unless its values are bit fields.
(Source: http://msdn.microsoft.com/en-us/library/ms229040.aspx)
...but not so clear for a class of constants (or read-only static fields/propertes). For example, should the name of this class be singular or plural?
public static class Token // or Tokens?
{
public const string Foo = "Foo";
public const string Bar = "Bar";
public const string Doo = "Doo";
public const string Hicky = "Hicky";
}
Upvotes: 30
Views: 9995
Reputation: 1
The question is quite old but still actual. I like to use plurals and not just because it feels more natural to me. If you create a nested enumeration in the class, you can't create the property with the same name. i.e.:
public class Token
{
public TokenType TokenType { get; set; }
public enum TokenType
{
Foo,
Bar
}
}
If you try to compile - you will get the compilation error "Error CS0102 The type 'Token' already contains a definition for 'TokenType'".
If you use singular for enum names - you need to come up with a unique name for the property, which is often inconvenient and unnatural.
But if you change the nested enumeration name to plural 'TokenTypes', there is no issue anymore. For consistency, I use the plural for each enumeration name, regardless of whether they are nested or not.
Upvotes: 0
Reputation: 3256
I would use the plural name: Tokens
However you may consider creating a Token
class for holding the const value.
This would be similar to System.Windows.Media.Colors
where e.g. Colors.Blue
returns a System.Windows.Media.Color
instance.
public class Token
{
public Token(string value)
{
Value = value;
}
public string Value { get; private set; }
public static implicit operator string(Token token)
{
return token == null ? null : token.Value;
}
public bool Equals(string value)
{
return String.Equals(Value, value);
}
public override bool Equals(object obj)
{
var other = obj as Token;
if (other == null)
{
return false;
}
return Equals(other.Value);
}
public override int GetHashCode()
{
return Value.GetHashCode();
}
public override string ToString()
{
return Value;
}
}
public static class Tokens
{
public static readonly Token Foo = new Token("Foo");
}
class Program
{
static void Main(string[] args)
{
// You can use it as if they were string constants.
string token = Tokens.Foo;
bool areEquals = String.Equals(token, Tokens.Foo);
}
}
Upvotes: 0
Reputation: 43076
I would use the plural: Tokens
. This implies that the static class is serving as a collection of items of some sort (whose runtime types are not that of the class).
On the other hand, an enumeration's fields are instances of the enumeration type. For example, TypeCode.String
is a TypeCode
. It would be weird to say that TypeCodes.String
is a TypeCodes
.
However, in your Tokens
example, using the singular gives us Token.Foo
, which is a token, but it is not a Token
(it is a string
).
(Or, if you use the plural class name, Tokens.Foo
is a string
, not a Tokens
. Ack!)
Upvotes: 37
Reputation: 16661
Since both are used essentially the same way, and are conceptually the same thing, I'd recommend just following the enum guidelines.
Upvotes: 6
Reputation: 22255
I don't have any official naming standard to link to, but I can tell you what I would do.
I would use the plural name: Tokens
Upvotes: 0