可通过所有枚举值,你循环? [英] Can you loop through all enum values?
本文介绍了可通过所有枚举值,你循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这个问题已经有了答案在这里:
如何枚举枚举?的 14的答案的
公开枚举FOOS
{
一个,
B,
C
}
有没有通过 FOOS
?
基本上?
的foreach(富在FOOS)
解决方案
是的,你可以使用GetValues方法
VAR值= Enum.GetValues(typeof运算(FOOS));
或者类型化版本
VAR值= Enum.GetValues(typeof运算(FOOS))演员LT; FOOS>();
我很久以前增加了一个辅助函数来我的私人图书馆,只是这样的场合
公共静态类EnumUtil {
公共静态的IEnumerable< T>的GetValues< T>(){
返回Enum.GetValues(typeof运算(T))铸造< T>();
}
}
用法:
VAR值= EnumUtil.GetValues< FOOS>();
This question already has an answer here:
How to enumerate an enum? 14 answers
public enum Foos
{
A,
B,
C
}
Is there a way to loop through the possible values of Foos
?
Basically?
foreach(Foo in Foos)
解决方案
Yes you can use the GetValues method
var values = Enum.GetValues(typeof(Foos));
Or the typed version
var values = Enum.GetValues(typeof(Foos)).Cast<Foos>();
I long ago added a helper function to my private library for just such an occasion
public static class EnumUtil {
public static IEnumerable<T> GetValues<T>() {
return Enum.GetValues(typeof(T)).Cast<T>();
}
}
Usage:
var values = EnumUtil.GetValues<Foos>();
这篇关于可通过所有枚举值,你循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文