C# 列表中的枚举值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11240236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:05:43  来源:igfitidea点击:

Enum values in a list

c#asp.net

提问by shanish

I've a enum class like,

我有一个枚举类,

public enum USERTYPE
{
   Permanant=1,
   Temporary=2,
}

in my business object I just declare this enum as

在我的业务对象中,我只是将这个枚举声明为

private List<USERTYPE> userType=new List<USERTYPE>;

and in the get/set method, I tried like

在 get/set 方法中,我尝试过

public List<USERTYPE> UserType
    {
        get 
        {
            return userType;
        }
        set
        { 
            userType= value; 
        }
    }

here it returns the no of rows as 0, how can I get all the values in the Enum here, can anyone help me here...

在这里它将行数返回为 0,我如何在这里获取 Enum 中的所有值,任何人都可以在这里帮助我...

采纳答案by Botz3000

You can use this to get all individual enum values:

您可以使用它来获取所有单独的枚举值:

private List<USERTYPE> userTypes = Enum.GetValues(typeof(USERTYPE)).Cast<USERTYPE>().ToList();

If you do things like this more often, you could create a generic utility method for it:

如果你更频繁地做这样的事情,你可以为它创建一个通用的实用方法:

public static T[] GetEnumValues<T>() where T : struct {
    if (!typeof(T).IsEnum) {
        throw new ArgumentException("GetValues<T> can only be called for types derived from System.Enum", "T");
    }
    return (T[])Enum.GetValues(typeof(T));
}

回答by Nikhil Agrawal

What you have is basically List of enum. Not individual items inside that enum.

你所拥有的基本上是枚举列表。不是该枚举中的单个项目。

To get list of enum values you can do

要获取您可以执行的枚举值列表

string[] str = Enum.GetNames(typeof(USERTYPE));

To use in get/set return string[] instead of List<>

在 get/set 中使用 return string[] 而不是 List<>

public string[] UserType
{
    get 
    {
        return Enum.GetNames(typeof(USERTYPE));
    }
}

I think set will not work here because you cannot add values in enum at runtime.

我认为 set 在这里不起作用,因为您无法在运行时在 enum 中添加值。

回答by oleksii

If you want to get specific enum value from the list user.UserTypethen you first need to Addenum value to this list:

如果要从列表中获取特定的枚举值,user.UserType则首先需要将Add值枚举到此列表:

var user = new User();
//1 value - PERMANENT
user.UserType.Add(USERTYPE.Permanent);

But if you only need to get all the possible values from an arbitrary enumthen you can try Enum.GetValues

但是如果您只需要从任意值中获取所有可能的值,enum那么您可以尝试Enum.GetValues

//2 values - Permanant and Temporary
var enums = Enum.GetValues(typeof(USERTYPE));

回答by Jeppe Stig Nielsen

GetValuesreturns System.Array, but we know it's really a TEnum[](that is a one-dimensional array indexed from zero) where TEnumis USERTYPEin your case. Therefore use:

GetValues回报System.Array,但我们知道这是一个真正的TEnum[](即从零索引的一维数组),其中TEnumUSERTYPE你的情况。因此使用:

var allUsertypeValues = (USERTYPE[])Enum.GetValues(typeof(USERTYPE));

回答by Md. Rashim Uddin

UserTypeCan you please try with that,

用户类型你可以试试吗,

 UserType = Enum.GetValues(typeof(USERTYPE)).OfType<USERTYPE>().ToList();