使用枚举值作为整数
我有以下枚举类型:
/// <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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以像转换为 int 一样转换为枚举类型,问题可能出在其他地方。
这会产生您所说的编译错误:
这不会:
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:
This would not:
那应该没问题。例如,编译没有问题:
您能否发布一个类似的简短但完整的程序,但无法编译?
您是否可能缺少
using System;
?That should be fine. For example, this compiles with no issues:
Could you post a similar short but complete program which fails to compile?
Are you perhaps missing a
using System;
?是的,您可以将枚举值转换为整数。如果 E_LAST 只是为了提供信息,您可以考虑执行类似的操作。
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.
这确实有效。以下编译良好:
这在运行时按预期打印“10”。
话虽如此,如果您打算这样做,我强烈建议您为所有枚举值赋值,而不仅仅是 E_FCM0。
This does work. The following compiles fine:
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.
如果您试图获取此枚举中的项目总数,您可能需要尝试使用:
另外,正如其他一些人指出的那样,将值分配给所有枚举值可能是一个好主意,以便关联的整数值如果中间添加一些东西,枚举值不会改变。如果将这些值写入数据库或文件,这可能会产生影响。
If you are trying to get the total number of items in this Enum, you might want to try using:
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.