What is the default value for enum variable?

前端 未结 3 966
花落未央
花落未央 2020-11-30 20:11

An enum variable, anyone know if it is always defaulting to the first element?

3条回答
  •  星月不相逢
    2020-11-30 21:03

    I think it's quite dangerous to rely on the order of the values in a enum and to assume that the first is always the default. This would be good practice if you are concerned about protecting the default value.

    enum E
    {
        Foo = 0, Bar, Baz, Quux
    }
    

    Otherwise, all it takes is a careless refactor of the order and you've got a completely different default.

提交回复
热议问题