By default, the associated value of enumeration member constants is of the type int
. The first variation starts at zero and is incremented by one for each following variant. Therefore, in this example:
enum TipoTelefone {
Residencial,
Celular,
Comercial
}
The value for each variant is: Residencial
, 0
; Celular
, 1
and Comercial
, 2
. Notice that it’s sequential.
But it is also possible to explicitly define values for members. For example:
enum ErrorCode {
Residencial = 100,
Celular = 200,
Comercial = 300
}
In this case, members have explicitly defined values. There is no longer an implicit sequence in the above example. In the above example, it seems a little unnecessary, but it may be useful in some specific situations.
In cases as shown in the example of the question, in which only the first element is redefined, the others continue to follow its order, in a sequential way.
Then, in the example below, the definition of Residencial
à 0
is redundant, since the first element is, by default, zero.
enum TipoTelefone {
Residencial = 0,
Celular,
Comercial
}
But if you do something like:
enum TipoTelefone {
Residencial = 5,
Celular,
Comercial
}
Variants shall be defined from 5. Residencial
, 5
; Celular
, 6
and Comercial
, 7
.