How to get an array of all enum values in C#?

前端 未结 10 2121
刺人心
刺人心 2020-12-05 01:48

I have an enum that I\'d like to display all possible values of. Is there a way to get an array or list of all the possible values of the enum instead of manually creating s

10条回答
  •  囚心锁ツ
    2020-12-05 02:14

    This gets you a plain array of the enum values using Enum.GetValues:

    var valuesAsArray = Enum.GetValues(typeof(Enumnum));
    

    And this gets you a generic list:

    var valuesAsList = Enum.GetValues(typeof(Enumnum)).Cast().ToList();
    

提交回复
热议问题