如何转换枚举在C#中的列表? [英] How do I convert an enum to a list in C#?

查看:233
本文介绍了如何转换枚举在C#中的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法来转换的枚举来包含所有枚举的选项列表?

Is there a way to convert an enum to a list that contains all the enum's options?

推荐答案

这将返回一个的IEnumerable< SomeEnum> 枚举的所有值

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

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

如果你想要的是一个名单,其中,SomeEnum&GT; ,只需添加 .ToList() .Cast&LT; SomeEnum&GT;()

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

要在您需要有 System.Linq的在使用部分数组使用CAST函数。

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

这篇关于如何转换枚举在C#中的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆