Dev*_*Son 2 c# enums unity-game-engine
我有个问题。
我目前正在研究枚举函数中的标志。然后我想知道,我可以指定为枚举标志的最大大小是多少?
我目前正在将标志标记如下。
A = 1 << 0,
B = 1 << 1,
C = 1 << 2,
ZZZ = 1 << MaxSize(?)
Run Code Online (Sandbox Code Playgroud)
我很好奇最大尺寸的值。
感谢您的阅读,由于是翻译,可能存在语法错误。:-)
以下是我的随意想法,排名不分先后:
enum的一组命名常量。
0)。enum可以使用任何 .NET 内置整数类型作为基础类型(截至 2021 年 .NET 5,我们仍然限制为与 2001 年相同的 8 个整数字节,但是我们最终可能会得到一个真正的结果Int128,但这还需要几年的时间):
Byte和SByte(1 个八位字节:8 位,分别为值0-255和)。-128127UInt16 和Int16(2 个字节又名 2 个八位字节:16 位,分别为值0-65535和-32768to 32767)。UInt32 和Int32(4 字节、32 位等)。UInt64 和Int64(8 字节、64 位等)。Boolean/bool用作枚举底层类型,但 C# 不允许这样做)。很明显,Int64and UInt64(又名longandulong对于那些对 Shift 键过敏的人来说)为位值提供了最宽的“空间”:毕竟有 64 位可供使用 - 而其他基础类型只授予您 32 位、 16 或区区 8 位。
an 中的成员数量没有限制enum,但显然具有较小基础类型的枚举将比较大类型更快地耗尽其可能值集,并最终会重复自身。
enum类型可以具有多个具有不同名称且共享相同整数值的成员,但在内部,CLR 无法区分其中一个成员。
我可以指定为枚举标志的最大大小是多少?
如果您指的是基础类型,那么简单的答案是Int64( long) 或UInt64( ulong)。
如果您询问枚举成员可以具有的最大值YourUnderlyingType.MaxValue,那么这将是,例如:
enum IntSizedEnum : Int32
{
LargestValue = Int32.MaxValue
}
enum BiggerEnum : UInt64
{
EvenLargerLargestValue = UInt64.MaxValue
}
Run Code Online (Sandbox Code Playgroud)
如果您询问设置的位数,那么如果您使用Int64或UInt64可以获得 64 位或单个标志 - 甚至使用位文字逐位定义它们,例如:
enum BiggerEnum : UInt64
{
All64Bits = 0b_11111111_11111111_11111111_11111111_11111111_11111111_11111111_11111111
}
Run Code Online (Sandbox Code Playgroud)
有了标志,你可能会变得有点疯狂:
[Flags]
enum BiggerEnum : UInt64
{
FlagsCombination = Bit_32 | Bit_33 | Bit_34,
Bit_0 = 0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000001,
Bit_1 = 0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000010,
Bit_2 = 0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000100,
Bit_3 = 0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00001000,
Bit_4 = 0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00010000,
Bit_5 = 0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00100000,
Bit_6 = 0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_01000000,
Bit_7 = 0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_10000000,
Bit_8 = 0b00000000_00000000_00000000_00000000_00000000_00000000_00000001_00000000,
Bit_9 = 0b00000000_00000000_00000000_00000000_00000000_00000000_00000010_00000000,
Bit_10 = 0b00000000_00000000_00000000_00000000_00000000_00000000_00000100_00000000,
Bit_11 = 0b00000000_00000000_00000000_00000000_00000000_00000000_00001000_00000000,
Bit_12 = 0b00000000_00000000_00000000_00000000_00000000_00000000_00010000_00000000,
Bit_13 = 0b00000000_00000000_00000000_00000000_00000000_00000000_00100000_00000000,
Bit_14 = 0b00000000_00000000_00000000_00000000_00000000_00000000_01000000_00000000,
Bit_15 = 0b00000000_00000000_00000000_00000000_00000000_00000000_10000000_00000000,
Bit_16 = 0b00000000_00000000_00000000_00000000_00000000_00000001_00000000_00000000,
Bit_17 = 0b00000000_00000000_00000000_00000000_00000000_00000010_00000000_00000000,
Bit_18 = 0b00000000_00000000_00000000_00000000_00000000_00000100_00000000_00000000,
Bit_19 = 0b00000000_00000000_00000000_00000000_00000000_00001000_00000000_00000000,
Bit_20 = 0b00000000_00000000_00000000_00000000_00000000_00010000_00000000_00000000,
Bit_21 = 0b00000000_00000000_00000000_00000000_00000000_00100000_00000000_00000000,
Bit_22 = 0b00000000_00000000_00000000_00000000_00000000_01000000_00000000_00000000,
Bit_23 = 0b00000000_00000000_00000000_00000000_00000000_10000000_00000000_00000000,
Bit_24 = 0b00000000_00000000_00000000_00000000_00000001_00000000_00000000_00000000,
Bit_25 = 0b00000000_00000000_00000000_00000000_00000010_00000000_00000000_00000000,
Bit_26 = 0b00000000_00000000_00000000_00000000_00000100_00000000_00000000_00000000,
Bit_27 = 0b00000000_00000000_00000000_00000000_00001000_00000000_00000000_00000000,
Bit_28 = 0b00000000_00000000_00000000_00000000_00010000_00000000_00000000_00000000,
Bit_29 = 0b00000000_00000000_00000000_00000000_00100000_00000000_00000000_00000000,
Bit_30 = 0b00000000_00000000_00000000_00000000_01000000_00000000_00000000_00000000,
Bit_31 = 0b00000000_00000000_00000000_00000000_10000000_00000000_00000000_00000000,
Bit_32 = 0b00000000_00000000_00000000_00000001_00000000_00000000_00000000_00000000,
Bit_33 = 0b00000000_00000000_00000000_00000010_00000000_00000000_00000000_00000000,
Bit_34 = 0b00000000_00000000_00000000_00000100_00000000_00000000_00000000_00000000,
Bit_35 = 0b00000000_00000000_00000000_00001000_00000000_00000000_00000000_00000000,
Bit_36 = 0b00000000_00000000_00000000_00010000_00000000_00000000_00000000_00000000,
Bit_37 = 0b00000000_00000000_00000000_00100000_00000000_00000000_00000000_00000000,
Bit_38 = 0b00000000_00000000_00000000_01000000_00000000_00000000_00000000_00000000,
Bit_39 = 0b00000000_00000000_00000000_10000000_00000000_00000000_00000000_00000000,
Bit_40 = 0b00000000_00000000_00000001_00000000_00000000_00000000_00000000_00000000,
Bit_41 = 0b00000000_00000000_00000010_00000000_00000000_00000000_00000000_00000000,
Bit_42 = 0b00000000_00000000_00000100_00000000_00000000_00000000_00000000_00000000,
Bit_43 = 0b00000000_00000000_00001000_00000000_00000000_00000000_00000000_00000000,
Bit_44 = 0b00000000_00000000_00010000_00000000_00000000_00000000_00000000_00000000,
Bit_45 = 0b00000000_00000000_00100000_00000000_00000000_00000000_00000000_00000000,
Bit_46 = 0b00000000_00000000_01000000_00000000_00000000_00000000_00000000_00000000,
Bit_47 = 0b00000000_00000000_10000000_00000000_00000000_00000000_00000000_00000000,
Bit_48 = 0b00000000_00000001_00000000_00000000_00000000_00000000_00000000_00000000,
Bit_49 = 0b00000000_00000010_00000000_00000000_00000000_00000000_00000000_00000000,
Bit_50 = 0b00000000_00000100_00000000_00000000_00000000_00000000_00000000_00000000,
Bit_51 = 0b00000000_00001000_00000000_00000000_00000000_00000000_00000000_00000000,
Bit_52 = 0b00000000_00010000_00000000_00000000_00000000_00000000_00000000_00000000,
Bit_53 = 0b00000000_00100000_00000000_00000000_00000000_00000000_00000000_00000000,
Bit_54 = 0b00000000_01000000_00000000_00000000_00000000_00000000_00000000_00000000,
Bit_55 = 0b00000000_10000000_00000000_00000000_00000000_00000000_00000000_00000000,
Bit_56 = 0b00000001_00000000_00000000_00000000_00000000_00000000_00000000_00000000,
Bit_57 = 0b00000010_00000000_00000000_00000000_00000000_00000000_00000000_00000000,
Bit_58 = 0b00000100_00000000_00000000_00000000_00000000_00000000_00000000_00000000,
Bit_59 = 0b00001000_00000000_00000000_00000000_00000000_00000000_00000000_00000000,
Bit_60 = 0b00010000_00000000_00000000_00000000_00000000_00000000_00000000_00000000,
Bit_61 = 0b00100000_00000000_00000000_00000000_00000000_00000000_00000000_00000000,
Bit_62 = 0b01000000_00000000_00000000_00000000_00000000_00000000_00000000_00000000,
Bit_63 = 0b10000000_00000000_00000000_00000000_00000000_00000000_00000000_00000000
}
Run Code Online (Sandbox Code Playgroud)
// Usage:
BiggerEnum value = default;
value |= BiggerEnum.Bit_32 | BiggerEnum.Bit_33 | BiggerEnum.Bit_34;
Console.WriteLine( value == BiggerEnum.FlagsCombination ); // true
Console.WriteLine( value ); // 30064771072
Run Code Online (Sandbox Code Playgroud)
...但你确实不应该这样做。
| 归档时间: |
|
| 查看次数: |
2600 次 |
| 最近记录: |