简体   繁体   English

如何创建枚举数组

[英]How to create an array of enums

I have about 30 different flagged enums that I would like to put into an array for indexing and quick access. 我有大约30个不同的标记枚举,我想将其放入数组中以进行索引和快速访问。 Let me also claify that I do not have 1 enum with 30 values but I have 30 enums with differing amounts of values. 我还要声明,我没有1个包含30个值的枚举,但我有30个具有不同数值的枚举。

The goal would be to add them to an array at a specifed index. 目标是将它们添加到指定索引处的数组中。 This way I can write a functions in which I can pass the array index into for setting particuler values of the enum. 这样我就可以编写一个函数,我可以在其中传递数组索引来设置枚举的粒度值。

Updated: Here is an example of what I am wanting to do. 更新:这是我想要做的一个例子。

enum main( enum1 = 0, enum2 = 1, enumn = n-1 ) - this has indexs which would match the index of the associated enum 枚举main(enum1 = 0,enum2 = 1,enumn = n-1) - 这个索引与相关枚举的索引相匹配

[flag] enum1(value1=0, value2=1, value3=2, value4=4...) [flag] enum1(value1 = 0,value2 = 1,value3 = 2,value4 = 4 ......)

[flag] enum2("") [flag] enum2(“”)

[flag] enum2("") [flag] enum2(“”)

since I am using flagable enums I have a class like the following 因为我使用的是可标记的枚举,所以我有一个如下的类

public static class CEnumWorker
{
   public static enum1 myEnum1 = enum1.value1;
   public static enum2 myEnum2 = enum2.value1;
   public static enumN myEnumN = enumN.value1;

   //I would then have functions that set the flags on the enums. I would like to access the enums through an array or other method so that I do not have to build a large switch statement to know which enum I am wanting to manipulate
}

Since you have 30 different types of enums, you can't create a strongly typed array for them. 由于您有30种不同类型的枚举,因此无法为它们创建强类型数组。 The best you could do would be an array of System.Enum: 你可以做的最好的是一个System.Enum数组:

Enum[] enums = new Enum[] { enum1.Value1, enum2.Value2, etc };

You would then have to cast when pulling an enum out of the array if you need the strongly typed enum value. 如果你需要强类型的枚举值,那么在从数组中拉出枚举时你必须进行强制转换。

If I understand correctly, you would have to do: 如果我理解正确,你必须这样做:

object[] enums = new object[30];
enums[0] = Enum1.Value1;
enums[1] = Enum2.AnotherValue;

But then you would have to access like this (not strongly typed, and easy to reference the wrong index): 但是你必须像这样访问(不是强类型,并且很容易引用错误的索引):

if ((Enum1)enums[0] == Enum1.Value1)
...

In .NET 4, you could use the Tuple: 在.NET 4中,您可以使用Tuple:

var enums = new Tuple<Enum1, Enum2>(Enum1.Value1, Enum2.AnotherValue);

if (enums.Item1 == Enum1.Value1)
...

...but I wouldn't recommend it for so many (30) enums, and I think there's even a limit of 8 or so. ...但我不推荐它用于这么多(30)枚举,我认为甚至有8个左右的限制。 You can also create your own class: 您还可以创建自己的类:

class Enums
{
  public Enum1 Enum1 { get; set; }
  public Enum2 Enum2 { get; set; }
}

Enums enums = new Enums();
enums.Enum1 = Enum1.Value1;
enums.Enum2 = Enum2.AnotherValue;

if (enums.Enum1 == Enum1.Value1)
...

I would recommend the last way because you're using a reference type, you're not dependent on index position, you can give the properties whatever name you want, and it's strongly typed. 我建议使用最后一种方法,因为你使用的是引用类型,你不依赖于索引位置,你可以为属性提供你想要的任何名称,并且它是强类型的。 The only thing you "lose" is the ability to easily iterate through the list, but you can achieve that with Reflection if you really need to. 你“失去”的唯一事情是能够轻松地遍历列表,但如果你真的需要,你可以用Reflection实现。

你总是可以使用好的旧object[] ,但这意味着要进行大量的渲染。

Enum provides two mechanisms for converting an integer to an enum value - the GetValues() method and plain casting: Enum提供了两种将整数转换为枚举值的机制 - GetValues()方法和普通转换:

enum EnumA { A1, A2, A1234 }
enum EnumB { B00, B01, B02, B04 }

class Program
{
    static void Main(string[] args)
    {
        EnumA a = ((EnumA[])Enum.GetValues(typeof(EnumA)))[0];

        Console.WriteLine(a);

        EnumB boa = (EnumB)3;

        Console.WriteLine(boa);
    }
}

I'm not quite sure why you want to create your own arrays if you can use one of these mechanisms to get an enum from an int. 如果您可以使用其中一种机制从int获取枚举,我不太清楚为什么要创建自己的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM