Convert Enum to List

This question already has an answer here:

  • How do I iterate over an enum? 26 answers
  • How do I convert an enum to a list in C#? [duplicate] 14 answers

  • Language[] result = (Language[])Enum.GetValues(typeof(Language))
    

    will get you your values, if you want a list of the enums.

    If you want a list of the names, use this:

    string[] names = Enum.GetNames(typeof(Languages));
    

    如果我正确理解你的要求,你正在寻找这样的东西

    var enumList = Enum.GetValues(typeof(Language)).OfType<Language>().ToList();
    

    If you want to store your enum elements in the list as Language type:

    Enum.GetValues(typeof(Language)).Cast<Language>().ToList();
    

    In case you want to store them as string:

    Enum.GetValues(typeof(Language)).Cast<Language>().Select(x => x.ToString()).ToList();
    
    链接地址: http://www.djcxy.com/p/16282.html

    上一篇: 迭代枚举类型

    下一篇: 将枚举转换为列表