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

后端 未结 7 1615
夕颜
夕颜 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:13

    What would be gained? You'd save a whopping 3 bytes of memory, at the cost of slightly slower execution and less intuitive or readable code. (Reading this, I have to wonder whether pyou actually had a reason for making it a byte, and what that reason might have been. Presumably you went out of your way to use a non-default type for a reason).

    If you plan to store millions of these things then yes, saving a few bytes on each may pay off. Otherwise, no.

    It's the same reason you don't typically use byte or short instead of int.

提交回复
热议问题