Integer to enum casting in C#
本问题已经有最佳答案,请猛点这里访问。
我有一个
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | enum EnumTest { Test1 = 0, Test2 = 1, Test3 = 1 << 1, Test4 = 1 << 2, Test5 = 1 << 12, Test6 = 1 << 13, Test7 = 1 << 20, Test8 = 1 << 22, Test9 = 1 << 23, Test10 = 1 << 24, Test11 = 1 << 25, Test12 = 1 << 14, Test13 = 1 << 15, Test14 = 1 << 16, Test15 = 1 << 25, Test16 = 1 << 17, Test17 = 1 << 18, Test18 = 1 << 19, Test19 = 1 << 21, Test20 = 1 << 26 } public void method1() { int number=67239937; EnumTest e = (EnumTest)number; //output: Test2|Test16|Test20 } |
假设你的
1 2 3 4 5 6 | enum E { A = 1 B = 2 C = 4 D = 8 } |
如果将每个值转换为二进制值,则可以得到:
1 2 3 4 | A = 0001 B = 0010 C = 0100 D = 1000 |
现在假设您尝试将
1 | E enumTest = (E)12; |
它将输出
在你的例子中,它只是试图找出哪个