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.Stringis 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!)
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);
}
}
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);
}
}
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.
发布评论
评论(5)
我会使用复数:
Tokens
。这意味着静态类充当某种类型的项目的集合(其运行时类型不是该类的运行时类型)。另一方面,枚举的字段是枚举类型的实例。例如,
TypeCode.String
是一个TypeCode
。说TypeCodes.String
是TypeCodes
会很奇怪。但是,在您的
Tokens
示例中,使用单数为我们提供了Token.Foo
,它是一个标记,但它不是一个令牌
(它是一个字符串
)。(或者,如果您使用复数类名称,则
Tokens.Foo
是一个string
,而不是Tokens
。确认!)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 aTypeCode
. It would be weird to say thatTypeCodes.String
is aTypeCodes
.However, in your
Tokens
example, using the singular gives usToken.Foo
, which is a token, but it is not aToken
(it is astring
).(Or, if you use the plural class name,
Tokens.Foo
is astring
, not aTokens
. Ack!)由于两者的使用方式本质上相同,并且在概念上是相同的,因此我建议仅遵循枚举指南。
Since both are used essentially the same way, and are conceptually the same thing, I'd recommend just following the enum guidelines.
我没有任何官方命名标准可以链接,但我可以告诉你我会做什么。
我会使用复数名称:Tokens
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
我会使用复数名称:
Tokens
但是您可以考虑创建一个
Token
类来保存 const 值。这与
System.Windows.Media.Colors
类似,其中Colors.Blue
返回一个System.Windows.Media.Color
实例。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 aSystem.Windows.Media.Color
instance.这个问题很老了,但仍然很现实。
我喜欢使用复数,不仅仅是因为它对我来说更自然。
如果在类中创建嵌套枚举,则无法创建同名的属性。
即:
如果您尝试编译 - 您将收到编译错误“错误 CS0102 类型‘Token’已包含‘TokenType’的定义”。
如果您使用单数作为枚举名称 - 您需要为该属性想出一个唯一的名称,这通常不方便且不自然。
但是,如果将嵌套枚举名称更改为复数“TokenTypes”,则不再有问题。为了保持一致性,我对每个枚举名称都使用复数,无论它们是否嵌套。
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.:
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.