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

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

问题描述

有没有办法将 enum 转换为包含所有枚举选项的列表?

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

推荐答案

这将返回一个 Enum 的所有值的 IEnumerable.

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

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

如果你希望它是一个 List,只需在 .Cast() 之后添加 .ToList().

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

要在数组上使用 Cast 函数,您需要在 using 部分中包含 System.Linq.

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

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

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