How do I convert an enum to a list in C#?
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.
🐳 作者:dupeng0811 📢 版权:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接 💬 留言:同时 , 如果文中有什么错误,欢迎指出。以免更多的人被误导。 |