C# Enum 忽略名称的顺序

标签 c# enums

我有下一个对象:

private enum Operation
{
        Power = 0x5E,             // ^
        Division = 0x2F,          // /
        Multiplication = 0x2A,    // *
        Subtraction = 0x2D,      // -
        Addition = 0x2B           // +
}

当我想用下一种方式将它转换成char[]时:

private static char[] GetOperators()
{
        List<char> ExistingOperators = new List<char>();

        foreach (Operation enumOperator in Enum.GetValues(typeof(Operation)))
        {
                ExistingOperators.Add((char)enumOperator);
                Console.WriteLine(enumOperator);
        }

        return ExistingOperators.ToArray<char>();
}

它在控制台上写入枚举的值,但按从小到大的顺序排列。 上面的例子输出:

Multiplication
Addition
Subtraction
Divison
Power

我想要实现的:(所以数组与enum声明的顺序相同)

char[] { '^', '/', '*', '*', '-', '*' };

提前致谢。

最佳答案

The elements of the array are sorted by the binary values of the enumeration constants (that is, by their unsigned magnitude).

来源:http://msdn.microsoft.com/en-us/library/system.enum.getvalues.aspx

关于C# Enum 忽略名称的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12768593/

相关文章:

c# - 手动激活/停用自定义控件的对齐线

properties - 使用枚举从 wicket 属性文件中选择字符串

ios - 嵌套枚举可以保存在 UserDefaults 中吗?

c# - ObjectPool<T> 或类似的 .NET 已经在库中了吗?

c# - 使用 waitone() 方法

c# - 脚本1015 : Unterminated string constant when loading string

scala - 通过 scala 或 java 中的继承扩展一组命名值

c++ - 通过模板-模板参数使用枚举标记对象

java - 在这种情况下使用合适的嵌套枚举?

c# - 单元测试Elastic Search Nest客户端-BulkAll