可通过所有枚举值,你循环?可通过

由网友(阳光一点的女生)分享简介:这个问题已经有了答案在这里: 如何枚举枚举?的 14的答案的公开枚举FOOS{一个,B,C}有没有通过 FOOS ?的可能值的方式来循环基本上?的foreach(富在FOOS)解决方案 是的,你可以使用GetValues​​方法VAR值= Enum.GetValues​​(typeof运算(FOOS));或者类型...

这个问题已经有了答案在这里:   如何枚举枚举?的 14的答案的

 公开枚举FOOS
{
    一个,
    B,
    C
}
 

有没有通过 FOOS

的可能值的方式来循环

基本上?

 的foreach(富在FOOS)
 

解决方案

是的,你可以使用GetValues​​方法

  VAR值= Enum.GetValues​​(typeof运算(FOOS));
 
自定义类型的详细讲解 2

或者类型化版本

  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>();

阅读全文

相关推荐

最新文章