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

后端 未结 7 1616
夕颜
夕颜 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:21

    If you are mapping the model with enum to another model, or serializing it, or your enum is reflected to the database column – then I would suggest you to specify the type explicitly.


    Scenario: You have a column in database: status_id with type tinyint. And you have enum in your code: enum Status { Well = 1, Bad = 2 }. And you use this enum in some entity. Let's say you use entity frameworks core 2.0. If you try to read/write this entity from database you will get the error "Unable to cast object", unless you specify the byte type explicitly.

提交回复
热议问题