仅包含字符串值的公共枚举

发布于 2024-12-08 18:28:30 字数 392 浏览 0 评论 0原文

我总是很困惑应该使用哪种枚举。一个哈希表,一个枚举,一个结构体,一个字典,一个数组(多么老派),静态字符串......

我不想在代码中使用字符串,而是想使用一个漂亮的枚举,如下所示:

public enum MyConfigs
{
    Configuration1,
    Configuration2
}

问题是我并不总是想要将我的枚举 toString() 转换为我对 enum 的索引表示不感兴趣。

表示基于字符串的值的公共枚举的最佳方式是什么?
最后,我希望在代码中需要的地方使用 MyConfigs.Configuration1

I'm always confused which kind of enumeration I should use. A hashtable, an enum, a struct a dictionary, an array (how oldschool), static strings...

Instead of using strings in my code I want to use a beautiful enum like so:

public enum MyConfigs
{
    Configuration1,
    Configuration2
}

Problem is that I don't always want to convert my enum toString() as I'm not interested in the index representation of the enum.

What is the best way to represent a public enumeration of string based values?
In the end I would love to end up with using MyConfigs.Configuration1 where needed in my code.

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

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

发布评论

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

评论(3

江城子 2024-12-15 18:28:30

我更喜欢将“分组”常量定义为虚拟静态类的静态成员,如下所示:

public static class FieldNames
{
    public const string BRANCH_CODE = "_frsBranchCode";
    public const string BATCH_ID = "_frsBatchId";
    public const string OFFICE_TYPE = "_frsOfficeType";
}

但当然它们不是直接“可枚举”的,因此除非您也提供静态数组,否则您不能对它们进行 foreach :

public static string[] AllFieldNames
{
    get
    {
        return new string[]
        {
            FieldNames.BRANCH_CODE,
            FieldNames.BATCH_ID,
            FieldNames.OFFICE_TYPE
        };
    }
}

I prefer defining "grouped" constants as static members of a dummy static class, like so:

public static class FieldNames
{
    public const string BRANCH_CODE = "_frsBranchCode";
    public const string BATCH_ID = "_frsBatchId";
    public const string OFFICE_TYPE = "_frsOfficeType";
}

But of course they are not "enumerable" directly, so you can't foreach over them unless you provide a static array too:

public static string[] AllFieldNames
{
    get
    {
        return new string[]
        {
            FieldNames.BRANCH_CODE,
            FieldNames.BATCH_ID,
            FieldNames.OFFICE_TYPE
        };
    }
}
べ繥欢鉨o。 2024-12-15 18:28:30
public static class MyConfigs
{
    public const string Configuration1 = "foo",
                        Configuration2 = "bar"
}

这与枚举的实现方式几乎相同(忽略整个“它必须是整数”的事情)。

public static class MyConfigs
{
    public const string Configuration1 = "foo",
                        Configuration2 = "bar"
}

This is then pretty-much identical to how enums are implemented (ignoring the whole "it must be an integer" thing).

時窥 2024-12-15 18:28:30

类型安全的枚举模式?

public class StringEnum
{
    #region Enum Values

    public static readonly StringEnum ValueOne = new StringEnum("Value One");
    public static readonly StringEnum ValueTwo = new StringEnum("Value Two");

    #endregion

    #region Enum Functionality

    public readonly string Value;

    private StringEnum(string value)
    {
        Value = value;
    }

    public override string ToString()
    {
        return value;
    }

    #endregion
}

您可以这样使用:

private void Foo(StringEnum enumVal)
{
  return "String value: " + enumVal;
}

如果您从不需要以类型安全的方式将这些值传递给方法等,那么最好只坚持使用常量文件。

Type-safe enum pattern?

public class StringEnum
{
    #region Enum Values

    public static readonly StringEnum ValueOne = new StringEnum("Value One");
    public static readonly StringEnum ValueTwo = new StringEnum("Value Two");

    #endregion

    #region Enum Functionality

    public readonly string Value;

    private StringEnum(string value)
    {
        Value = value;
    }

    public override string ToString()
    {
        return value;
    }

    #endregion
}

You can use this like:

private void Foo(StringEnum enumVal)
{
  return "String value: " + enumVal;
}

If you never need to pass these values around in a type-safe manner to methods etc. then it is probably best to just stick with a constants file.

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