- 3

# What is the output of this code? enum Test {a=2,b,c,d,e}; static void main (string [] args){int(x) = (int)Test.c ; writeln (x)}

answer that came up was 4 how??

4 Answers

+ 3

It would be error, those parenthesis when creating the int are not valid.
Anywho, for enums they start at the value of 0 by default, each increasing by one.
So:
enum Test{
a,
b,
c,
}
Is the same as writing:
enum Test{
a = 0,
b = 1,
c = 2
}
If you modify the default value, everything after that will keep incrementing as usual.
So:
enum Test{
a = 2,
b,
c
}
Is really the same as:
enum Test{
a = 2,
b = 3,
c = 4
}
So, the c is 4.

- 2

c is 4

- 2

Answer 4

- 2

answer: 4