Enum inheriting from int

前端 未结 9 1251
借酒劲吻你
借酒劲吻你 2020-12-17 08:43

I\'ve found this all over the place in this code:

public enum Blah: int
{
    blah = 0,
    blahblah = 1
}

Why would it need to inherit fro

相关标签:
9条回答
  • 2020-12-17 09:21

    You don't need to inherit from int but by default it does. You can inherit from other integral types (byte, long, etc) if you want to. An example would be if you wanted to save memory or column space in a DB.

    0 讨论(0)
  • 2020-12-17 09:24

    Enums are implicitly backed by integers.
    : int just restates the default, just like void M(); vs. private void M();.

    You can also create enums that are backed by other intergral types, such as enum GiantEnum : long.

    0 讨论(0)
  • 2020-12-17 09:26

    Most of the time I don’t care if an enum is signed or unsigned, or how many bits it has, so I just let the system use it’s default that is int.

    However there are times when I do care that the enum is a signed 32 bit int, and then it is good to make it clear that I do care. I would expect a comment as well spelling out why I care.

    0 讨论(0)
提交回复
热议问题