C#.NET enums: choose all correct placement and typing rules.
-
A1, 3, 4
-
B2, 5
-
C3, 4
-
D2, 4, 5
Answer
Correct Answer: 1, 3, 4
Explanation
Introduction / Context:This question validates knowledge of where enums can be declared and which underlying numeric types are allowed in C#.NET.
Given Data / Assumptions (Statements):
- 1) An enum can be declared inside a class.
- 2) An enum can take Single, Double or Decimal values.
- 3) An enum can be declared outside a class.
- 4) An enum can be declared inside/outside a namespace.
- 5) An object can be assigned to an enum variable.
Concept / Approach:Enums can be declared at namespace scope or nested inside classes/structs. Underlying types must be integral: byte, sbyte, short, ushort, int, uint, long, or ulong. Floating-point and decimal are not allowed. Enum variables must hold values of that enum’s underlying integral domain, not arbitrary object references.
Step-by-Step Evaluation:
1) True: nested enums are valid.2) False: Single, Double, Decimal are not permitted as underlying types.3) True: enums can be declared at the top level (namespace scope).4) True: both inside a type and at namespace level are allowed.5) False: anobject cannot be directly assigned to an enum variable.Verification / Alternative check:Declare test enums with : byte or : long to confirm valid underlying types. Attempting : float or : decimal results in a compile error.
Why Other Options Are Wrong:Any choice including statement 2 or 5 is invalid due to incorrect typing rules for enums.
Common Pitfalls:Assuming any numeric type works; only integral types are valid. Also, forgetting that nested enums default to member accessibility rules of the containing type.
Final Answer:1, 3, 4