C# 中常量类的命名约定:复数还是单数?

发布于 2024-12-12 20:22:05 字数 1429 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情栀口红 2024-12-19 20:22:05

我会使用复数:Tokens。这意味着静态类充当某种类型的项目的集合(其运行时类型不是该类的运行时类型)。

另一方面,枚举的字段是枚举类型的实例。例如,TypeCode.String一个TypeCode。说 TypeCodes.StringTypeCodes 会很奇怪。

但是,在您的 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 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!)

情释 2024-12-19 20:22:05

由于两者的使用方式本质上相同,并且在概念上是相同的,因此我建议仅遵循枚举指南。

Since both are used essentially the same way, and are conceptually the same thing, I'd recommend just following the enum guidelines.

攀登最高峰 2024-12-19 20:22:05

我没有任何官方命名标准可以链接,但我可以告诉你我会做什么。

我会使用复数名称: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

泛泛之交 2024-12-19 20:22:05

我会使用复数名称:Tokens

但是您可以考虑创建一个 Token 类来保存 const 值。

这与System.Windows.Media.Colors 类似,其中Colors.Blue 返回一个System.Windows.Media.Color 实例。

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);
    }
}
人间不值得 2024-12-19 20:22:05

这个问题很老了,但仍然很现实。
我喜欢使用复数,不仅仅是因为它对我来说更自然。
如果在类中创建嵌套枚举,则无法创建同名的属性。
即:

public class Token
{
    public TokenType TokenType { get; set; }

    public enum TokenType
    {
        Foo,
        Bar
    }
}

如果您尝试编译 - 您将收到编译错误“错误 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.:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文