gpt4 book ai didi

C# Enum 忽略名称的顺序

转载 作者:太空宇宙 更新时间:2023-11-03 18:37:52 24 4
gpt4 key购买 nike

我有下一个对象:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com