When declaring an enum, should you force the type to byte for under 256 entities?

后端 未结 7 1601
夕颜
夕颜 2020-12-13 08:51

If you have an enum in your application and you only have a few items, should you force the underlying type to be the smallest possible type?

    enum smalle         


        
7条回答
  •  离开以前
    2020-12-13 09:23

    You should not assign a certain integer type to enumerations and let C# fall back to the default int1 but let the .NET environment figure out the best "size" for the enum. As JaredPar said, if you change the data type, you should definitely check whether it actually helps.

    The thing is that 32-bit integers are "natural" on x86 processors because they can be easily align in an optimal fashion.

    1 By default, the associated constant values of enum members are of type int

提交回复
热议问题