开发者

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

开发者 https://www.devze.com 2023-01-22 04:06 出处:网络
enum Orientation { None = -1, North = 0, East = 1, South = 2, West = 3 } how to iter开发者_如何学编程ate this enum gives all values from none to westUse Enum.GetValues:
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

精彩评论

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