C#.NET enums: select all correct statements about their nature and printing behavior.

Difficulty: Easy

Correct Answer: 1, 2, 5

Explanation:


Introduction / Context:
This checks foundational facts about enums in C#.NET: their inheritance, value/reference nature, printing, and default underlying type.



Given Data / Assumptions (Statements):

  • 1) Every enum is derived from Object.
  • 2) Every enum is a value type.
  • 3) There is no way to print an enum element as a string.
  • 4) Every enum is a reference type.
  • 5) The default underlying datatype of an enum is int.


Concept / Approach:
An enum type ultimately derives from System.Enum, which inherits from System.ValueType, then System.Object. Therefore, enums are value types. The default underlying type is int unless specified otherwise. Enum values can be converted to strings via .ToString() or Enum.GetName.



Step-by-Step Evaluation:

1) True: All types ultimately derive from System.Object.2) True: Enums are value types by definition (ValueType).3) False: myEnumValue.ToString() prints the name; formatting options exist.4) False: Enums are not reference types.5) True: Default underlying type is int.


Verification / Alternative check:
Run Console.WriteLine(MyEnum.SomeValue.ToString()) or use Enum.GetNames(typeof(MyEnum)) to list names.



Why Other Options Are Wrong:
Any option containing 3 or 4 contradicts well-documented enum behavior in .NET.



Common Pitfalls:
Confusing the ability to print names with numeric casts; both are available but serve different purposes.



Final Answer:
1, 2, 5

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion