I have the following enumerated type:
/// <summary>
开发者_Go百科 /// 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?
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);
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;
?
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 );
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.
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.
精彩评论