欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

How do I convert an enum to a list in C#?

程序员文章站 2022-04-24 14:19:26
...

How do I convert an enum to a list in C#?

This will return an IEnumerable<SomeEnum> of all the values of an Enum.

Enum.GetValues(typeof(SomeEnum)).Cast<SomeEnum>();

If you want that to be a List<SomeEnum>, just add .ToList() after .Cast<SomeEnum>().

To use the Cast function on an Array you need to have the System.Linq in your using section.

转载于:https://www.cnblogs.com/dupeng0811/p/how-do-i-convert-an-enum-to-a-list-in-c.html