我有下一个对象:
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/