开发者

how to iterate this enum gives all values from none to west

enum Orientation
{
    None = -1,
    North = 0,
    East = 1,
    South = 2,
    West = 3
}

how to iter开发者_如何学编程ate this enum gives all values from none to west


Use Enum.GetValues:

Orientation[] orientations = (Orientation[]) Enum.GetValues(typeof(Orientation));

Note that 0 is a more conventional value for None, as it will be the default value for any instance/static fields of type Orientation, as well as initial values in arrays etc.

EDIT: Enum.GetValues is documented to return the values "sorted by the binary values of the enumeration constants" - and I believe it's treating them as unsigned values.

There's a simple answer to this, fortunately:

Orientation[] orientations = (Orientation[]) Enum.GetValues(typeof(Orientation));
Array.Sort(orientations);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜