What's the use-case for specifying the underlying type in enums?

后端 未结 3 1068
名媛妹妹
名媛妹妹 2021-01-04 01:35

What is the point of having

enum SomeEnum : byte // <----
{
  SomeValue = 0x01,
  ...
}

when you have to make a cast just to assign it t

3条回答
  •  抹茶落季
    2021-01-04 01:46

    Not much point, really, except that if the default underlying type (int) is not enough for you, ie. you want to use higher integer values than that then you can make it long. This can be useful when you have a [Flags] enum with more than 32 values.

    You can make it byte or short just to restrict the range of values, but it will actually still take 4 bytes (ie. same as int).

提交回复
热议问题