为什么 Enum.GetValues 比迭代更好?
Why is Enum.GetValues better than iterating?
我正在阅读this答案
By the way, incrementing the value is not a good way to enumerate the
values of an enum. You should do this instead.
I would use Enum.GetValues(typeof(Suit))
instead.
为什么? GetValues
比迭代有何优势?
看来你误解了那里的上下文。
您所指的答案的海报建议使用 GetNames
遍历枚举的 names。但是他说,当谈到 值 时,枚举超过 GetValues
比递增更好。
所以一个并不比另一个好,它们很好地服务于不同的目的,答案的作者概述了这一点。
正如@knittl 所述:
Because you can have "holes" in your enum. Consider enum E { A = 1, B =
2, C = 4 }. Incrementing will yield A, B, 3, C enumerating over all
values will yield A, B, C
感谢@Andrei 指出:
enum might be defined with any values. Even with chars. So
incrementing might give you a lot of stuff that is no in the enum.
GetValues is guaranteed to return only relevant values though
我正在阅读this答案
By the way, incrementing the value is not a good way to enumerate the values of an enum. You should do this instead.
I would use
Enum.GetValues(typeof(Suit))
instead.
为什么? GetValues
比迭代有何优势?
看来你误解了那里的上下文。
您所指的答案的海报建议使用 GetNames
遍历枚举的 names。但是他说,当谈到 值 时,枚举超过 GetValues
比递增更好。
所以一个并不比另一个好,它们很好地服务于不同的目的,答案的作者概述了这一点。
正如@knittl 所述:
Because you can have "holes" in your enum. Consider enum E { A = 1, B = 2, C = 4 }. Incrementing will yield A, B, 3, C enumerating over all values will yield A, B, C
感谢@Andrei 指出:
enum might be defined with any values. Even with chars. So incrementing might give you a lot of stuff that is no in the enum. GetValues is guaranteed to return only relevant values though