Iteration on enum?

Possible Duplicate:
C#: How to enumerate an enum?

Hi all I want to know that suppose I define a enum like

enum color
{
    red=4;
    blue=5;
    gray=6;
    green=8;
}

so can we print value of constant with the help of for loop, i means can we control it by loop; another question can we integrate it with dropdown list like as array. i means when we declare a element in side array suppose i write same color element, and those elment we can add with list or dropdown list(simple word binding with control)in asp.net , same thing can we perform with enum.


Use Enum.GetValues() :

Color[] colors = (Color[]) Enum.GetValues(typeof(Color));

I suspect that may have answered all your questions, but I didn't really follow the second half. If you still need help, please edit your question to be clearer.


使用静态Enum.GetValues()方法:

foreach (color value in Enum.GetValues(typeof(color)))
{
  //Do something here
}
链接地址: http://www.djcxy.com/p/16296.html

上一篇: 迭代枚举的方法?

下一篇: 迭代枚举?