[英]arithmetic operations on enum values
What arithmetic operations are supported on c# enums? c#枚举支持哪些算术运算? Surprisingly, I was unable to find it via neither google, nor wikipedia and stackoverflow.
令人惊讶的是,我既无法通过Google也无法通过Wikipedia和stackoverflow找到它。
Can I add two enum values without any cast ? 我可以在不进行任何强制转换的情况下添加两个枚举值吗? Add arbitrary constant to a value or subtract it?
在值上加上或减去任意常数? Or does enum guarantee that a value of that type is always one of the defined enum values or their bitmask?
还是枚举确保该类型的值始终是已定义枚举值或其位掩码之一?
class ... {...
enum WeekDays : byte { Sun = 1, Mon = 2, Tue = 3, /* and so on*/ Sat = 7 };
public static bool IsWeekend (WeekDays _d) {
/// Can I be sure here that _d has value from 1..7? May it be any of 0..255?
}
I know about bitwise operations, It seems reasonable to support them for representing flags. 我知道按位运算,支持它们表示标志似乎是合理的。
Wikipedia tells us, my sample also allows _d - 1
or WeekDays.Tue - WeekDays.Mon
, that can be useful for strictly ordered sequential enums, but I cannot find any standard reference, could you, please, point me? Wikipedia告诉我们,我的示例还允许
_d - 1
或WeekDays.Tue - WeekDays.Mon
,这对于严格排序的顺序枚举很有用,但是我找不到任何标准参考,请给我指出吗?
The following operators can be used on values of enum types: ==
, !=
, <
, >
, <=
, >=
, +
, -
, ^
, &
, |
以下运算符可用于枚举类型的值:
==
, !=
, <
, >
, <=
, >=
, +
, -
, ^
, &
, |
, ~
, ++
, --
, sizeof
. ,
~
, ++
, --
, sizeof
。
If you want to use an arithmetic operations, do not use enums
, use numbers
. 如果要使用算术运算,请不要使用
enums
,而应使用numbers
。 Enums
is a naming convension for numeric values to make them more human readable and allow having a combination of them. Enums
是数字值的命名惯例,以使它们更易于阅读并允许将它们组合使用。 That is actually the reason that you didn't find anything about that on internet, cause it's not soemthing that should be done with enums
. 实际上,这就是您在互联网上找不到任何相关信息的原因,因为它不应该用
enums
来完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.