C#.NET enum with negative starting value: what integers are printed? enum color : int { red = -3, green, blue } Console.Write((int)color.red + ", "); Console.Write((int)color.green + ", "); Console.Write((int)color.blue);
-
A-3, -2, -1
-
B-3, 0, 1
-
C0, 1, 2
-
Dred, green, blue
-
Ecolor.red, color.green, color.blue
Answer
Correct Answer: -3, -2, -1
Explanation
Introduction / Context:This checks whether you know that enum underlying values in C# can be any valid integral values, including negatives, and that implicit members increment by 1 from the prior member.
Given Data / Assumptions:
red = -3is explicitly assigned.greenandblueare implicit following members.- Underlying type is
int.
Concept / Approach:An enum member without an explicit value receives the value of the previous member plus 1, even when starting from a negative value.
Step-by-Step Solution:
red → -3 (explicit).green → -2 (implicit: -3 + 1).blue → -1 (implicit: -2 + 1).Printing yields -3, -2, -1.Verification / Alternative check:Cast each member to int in a quick console app, or iterate Enum.GetValues(typeof(color)) and print casted values.
Why Other Options Are Wrong:-3, 0, 1 and 0, 1, 2 assume automatic reset at 0, which does not happen. red, green, blue and fully qualified names are not integers.
Common Pitfalls:Thinking that enums must start at 0 or that negatives are disallowed; both are misconceptions.
Final Answer:-3, -2, -1