相关疑难解决方法(0)

如何将枚举转换为C#中的列表?

有没有办法将一个转换为enum包含所有枚举选项的列表?

.net c# enums

572
推荐指数
10
解决办法
37万
查看次数

将枚举转换为列表

假设我有以下枚举值

enum Language
    {
       CSharp= 0,
        Java = 1,
        VB = 2

    }
Run Code Online (Sandbox Code Playgroud)

我想将它们转换为值列表(即) { CSharp,Java,VB}.

如何将它们转换为值列表?

c# enums list type-conversion

30
推荐指数
3
解决办法
5万
查看次数

将枚举值转换为字符串数组

public enum VehicleData
{
    Dodge = 15001,
    BMW = 15002,
    Toyota = 15003        
}
Run Code Online (Sandbox Code Playgroud)

我想在字符串数组中得到值15001,15002,15003,如下所示:

string[] arr = { "15001", "15002", "15003" };
Run Code Online (Sandbox Code Playgroud)

我试过下面的命令,但这给了我一些名字而不是值.

string[] aaa = (string[]) Enum.GetNames(typeof(VehicleData));
Run Code Online (Sandbox Code Playgroud)

我也尝试过,string[] aaa = (string[]) Enum.GetValues(typeof(VehicleData));但也没有用.

有什么建议?

c# linq enums

14
推荐指数
3
解决办法
2万
查看次数

如何创建枚举数组

我有大约30个不同的标记枚举,我想将其放入数组中以进行索引和快速访问.我还要声明,我没有1个包含30个值的枚举,但我有30个具有不同数值的枚举.

目标是将它们添加到指定索引处的数组中.这样我就可以编写一个函数,我可以在其中传递数组索引来设置枚举的粒度值.

更新:这是我想要做的一个例子.

枚举main(enum1 = 0,enum2 = 1,enumn = n-1) - 这个索引与相关枚举的索引相匹配

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

[flag] enum2("")

[flag] enum2("")

因为我使用的是可标记的枚举,所以我有一个如下的类

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 …
Run Code Online (Sandbox Code Playgroud)

c# arrays enums

9
推荐指数
1
解决办法
5万
查看次数

标签 统计

c# ×4

enums ×4

.net ×1

arrays ×1

linq ×1

list ×1

type-conversion ×1