使用枚举值作为整数

发布于 2024-09-06 14:10:27 字数 1854 浏览 2 评论 0原文

我有以下枚举类型:

        /// <summary>
        /// TTE Node types.
        /// </summary>
        public enum E_TTE_NODES
        {
            /// <summary>
            /// Represents FCM 0
            /// </summary>
            E_FCM0 = 0,

            /// <summary>
            /// Represents FCM 1
            /// </summary>
            E_FCM1,

            /// <summary>
            /// Represents FCM 2
            /// </summary>
            E_FCM2,

            /// <summary>
            /// Represents DCM 0
            /// </summary>
            E_DCM0,

            /// <summary>
            /// Represents DCM 1
            /// </summary>
            E_DCM1,

            /// <summary>
            /// Represents DCM 2
            /// </summary>
            E_DCM2,

            /// <summary>
            /// Represents CCM 0
            /// </summary>
            E_CCM0,

            /// <summary>
            /// Represents CCM 1
            /// </summary>
            E_CCM1,

            /// <summary>
            /// Represents CCM 2
            /// </summary>
            E_CCM2,

            /// <summary>
            /// Represents PDU C1
            /// </summary>
            E_PDU_C1,

            /// <summary>
            /// Represents the last node.
            /// Must remain last.
            /// </summary>
            E_LAST,         
        }

我想初始化一个像这样的通用列表:

// Should initialize to a capacity of 10
private List<Int32> transmitIndex = new List<Int32>((Int32)E_TTE_NODES.E_LAST);

是的,我知道我可以将数字 10 作为参数传递。枚举将来可能会添加更多节点,但 E_LAST 将始终是最后一个节点。我的问题是编译器是否说我无法将上面代码行的枚举转换为 int 。枚举值的默认值不是整数吗?

I have the following enumerated type:

        /// <summary>
        /// TTE Node types.
        /// </summary>
        public enum E_TTE_NODES
        {
            /// <summary>
            /// Represents FCM 0
            /// </summary>
            E_FCM0 = 0,

            /// <summary>
            /// Represents FCM 1
            /// </summary>
            E_FCM1,

            /// <summary>
            /// Represents FCM 2
            /// </summary>
            E_FCM2,

            /// <summary>
            /// Represents DCM 0
            /// </summary>
            E_DCM0,

            /// <summary>
            /// Represents DCM 1
            /// </summary>
            E_DCM1,

            /// <summary>
            /// Represents DCM 2
            /// </summary>
            E_DCM2,

            /// <summary>
            /// Represents CCM 0
            /// </summary>
            E_CCM0,

            /// <summary>
            /// Represents CCM 1
            /// </summary>
            E_CCM1,

            /// <summary>
            /// Represents CCM 2
            /// </summary>
            E_CCM2,

            /// <summary>
            /// Represents PDU C1
            /// </summary>
            E_PDU_C1,

            /// <summary>
            /// Represents the last node.
            /// Must remain last.
            /// </summary>
            E_LAST,         
        }

I would like to initialize a generic list like this:

// Should initialize to a capacity of 10
private List<Int32> transmitIndex = new List<Int32>((Int32)E_TTE_NODES.E_LAST);

Yes, I know I can just pass the number 10 as a parameter. The enum may add more nodes in the future, but E_LAST will always be the last node. My question is my does the compiler say I cannot cast my enum to an int on the above line of code. Isn't the default value of a enum value an integer?

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

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

发布评论

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

评论(5

一杯敬自由 2024-09-13 14:10:27

您可以像转换为 int 一样转换为枚举类型,问题可能出在其他地方。

这会产生您所说的编译错误:

 private List<Int32> transmitIndex = new List<Int32>(E_TTE_NODES.E_LAST);

这不会:

 private List<Int32> transmitIndex = new List<Int32>((Int32)E_TTE_NODES.E_LAST);

You can cast an enum type as you have to an int, the problem is probably somewhere else.

This would produce the compiling error you said:

 private List<Int32> transmitIndex = new List<Int32>(E_TTE_NODES.E_LAST);

This would not:

 private List<Int32> transmitIndex = new List<Int32>((Int32)E_TTE_NODES.E_LAST);
悲念泪 2024-09-13 14:10:27

那应该没问题。例如,编译没有问题:

using System;
using System.Collections.Generic;

enum Foo
{
    Bar = 0,
    Baz
}

public class Test
{
    static void Main()
    {
        List<Int32> transmitIndex = new List<Int32>((Int32)Foo.Baz);
    }
}

您能否发布一个类似的简短但完整的程序,但无法编译?

您是否可能缺少using System;

That should be fine. For example, this compiles with no issues:

using System;
using System.Collections.Generic;

enum Foo
{
    Bar = 0,
    Baz
}

public class Test
{
    static void Main()
    {
        List<Int32> transmitIndex = new List<Int32>((Int32)Foo.Baz);
    }
}

Could you post a similar short but complete program which fails to compile?

Are you perhaps missing a using System;?

自演自醉 2024-09-13 14:10:27

是的,您可以将枚举值转换为整数。如果 E_LAST 只是为了提供信息,您可以考虑执行类似的操作。

string numberOfElements = Enum.GetNames( typeof( E_TTE_NODES ) ).Length;
List<Int32> transmitIndex = new List<Int32>( numberOfElements );

Yes you can cast an enum value to and integer. If E_LAST is just for information purpose you could look at doing something like this.

string numberOfElements = Enum.GetNames( typeof( E_TTE_NODES ) ).Length;
List<Int32> transmitIndex = new List<Int32>( numberOfElements );
夜血缘 2024-09-13 14:10:27

这确实有效。以下编译良好:

using System;
using System.Collections.Generic;

/// <summary>
/// TTE Node types.
/// </summary>
public enum E_TTE_NODES
{
    /// <summary>
    /// Represents FCM 0
    /// </summary>
    E_FCM0 = 0,

    /// <summary>
    /// Represents FCM 1
    /// </summary>
    E_FCM1,

    /// <summary>
    /// Represents FCM 2
    /// </summary>
    E_FCM2,

    /// <summary>
    /// Represents DCM 0
    /// </summary>
    E_DCM0,

    /// <summary>
    /// Represents DCM 1
    /// </summary>
    E_DCM1,

    /// <summary>
    /// Represents DCM 2
    /// </summary>
    E_DCM2,

    /// <summary>
    /// Represents CCM 0
    /// </summary>
    E_CCM0,

    /// <summary>
    /// Represents CCM 1
    /// </summary>
    E_CCM1,

    /// <summary>
    /// Represents CCM 2
    /// </summary>
    E_CCM2,

    /// <summary>
    /// Represents PDU C1
    /// </summary>
    E_PDU_C1,

    /// <summary>
    /// Represents the last node.
    /// Must remain last.
    /// </summary>
    E_LAST,
}

public class Example
{
    private List<Int32> transmitIndex = new List<Int32>((Int32) E_TTE_NODES.E_LAST);

    public static void Main()
    {
        Example example = new Example();
        Console.WriteLine(example.transmitIndex.Capacity);

        Console.ReadKey();
    }
}

这在运行时按预期打印“10”。

话虽如此,如果您打算这样做,我强烈建议您为所有枚举值赋值,而不仅仅是 E_FCM0。

This does work. The following compiles fine:

using System;
using System.Collections.Generic;

/// <summary>
/// TTE Node types.
/// </summary>
public enum E_TTE_NODES
{
    /// <summary>
    /// Represents FCM 0
    /// </summary>
    E_FCM0 = 0,

    /// <summary>
    /// Represents FCM 1
    /// </summary>
    E_FCM1,

    /// <summary>
    /// Represents FCM 2
    /// </summary>
    E_FCM2,

    /// <summary>
    /// Represents DCM 0
    /// </summary>
    E_DCM0,

    /// <summary>
    /// Represents DCM 1
    /// </summary>
    E_DCM1,

    /// <summary>
    /// Represents DCM 2
    /// </summary>
    E_DCM2,

    /// <summary>
    /// Represents CCM 0
    /// </summary>
    E_CCM0,

    /// <summary>
    /// Represents CCM 1
    /// </summary>
    E_CCM1,

    /// <summary>
    /// Represents CCM 2
    /// </summary>
    E_CCM2,

    /// <summary>
    /// Represents PDU C1
    /// </summary>
    E_PDU_C1,

    /// <summary>
    /// Represents the last node.
    /// Must remain last.
    /// </summary>
    E_LAST,
}

public class Example
{
    private List<Int32> transmitIndex = new List<Int32>((Int32) E_TTE_NODES.E_LAST);

    public static void Main()
    {
        Example example = new Example();
        Console.WriteLine(example.transmitIndex.Capacity);

        Console.ReadKey();
    }
}

This prints "10" as expected, at runtime.

That being said, I'd strongly recommend assigning values to all of your enum values, not just E_FCM0, if you're going to do this.

温柔嚣张 2024-09-13 14:10:27

如果您试图获取此枚举中的项目总数,您可能需要尝试使用:

Enum.GetValues(typeof(E_TTE_NODES)).Length

另外,正如其他一些人指出的那样,将值分配给所有枚举值可能是一个好主意,以便关联的整数值如果中间添加一些东西,枚举值不会改变。如果将这些值写入数据库或文件,这可能会产生影响。

If you are trying to get the total number of items in this Enum, you might want to try using:

Enum.GetValues(typeof(E_TTE_NODES)).Length

Also, as some others have pointed out, it's probably a good idea to assign values to all your enum values so that the integer values associated with the enum values don't change if something is added in the middle. This could make a difference if you write these values out to a database, or a file.

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