Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Underlying numeric values in C Sharp (C#) - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Underlying numeric values
Define enum with named constants
↓
Assign enum variable a named constant
↓
Convert enum to underlying numeric value
↓
Use numeric value in code or output
This flow shows how an enum variable is assigned a named constant and then converted to its underlying numeric value.
Execution Sample
C Sharp (C#)
enum Color { Red = 1, Green = 2, Blue = 3 }
Color c = Color.Green;
int numericValue = (int)c;
Console.WriteLine(numericValue);
This code defines an enum Color, assigns Green to c, converts it to int, and prints the numeric value.
Execution Table
StepActionVariableValueExplanation
1Define enum ColorColorRed=1, Green=2, Blue=3Enum named constants assigned numeric values
2Assign Color.Green to ccGreenVariable c holds enum value Green
3Convert c to intnumericValue2Green corresponds to numeric value 2
4Print numericValueOutput2Output shows underlying numeric value of Green
💡 Program ends after printing the numeric value 2
Variable Tracker
VariableStartAfter Step 2After Step 3Final
cundefinedGreenGreenGreen
numericValueundefinedundefined22
Key Moments - 2 Insights
Why do we cast the enum variable to int before printing?
Because enum variables hold named constants, not numbers directly. Casting to int extracts the underlying numeric value, as shown in step 3 of the execution_table.
Can enum values be used directly as numbers without casting?
No, enum values are strongly typed. You must cast them to their underlying numeric type to use them as numbers, as demonstrated in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'c' after step 2?
AGreen
BRed
CBlue
D2
💡 Hint
Check the 'Variable' and 'Value' columns for step 2 in the execution_table.
At which step is the enum variable converted to its numeric value?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for the step where 'numericValue' is assigned in the execution_table.
If we change Color.Green to have value 5, what will numericValue be after step 3?
A2
B3
C5
DGreen
💡 Hint
Refer to variable_tracker and understand that numericValue matches the enum's underlying number.
Concept Snapshot
enum Color { Red=1, Green=2, Blue=3 }
Color c = Color.Green; // c holds named constant
int numericValue = (int)c; // cast to get underlying number
Console.WriteLine(numericValue); // prints 2

Key: Enum values have numeric underlying values; cast to int to use them as numbers.
Full Transcript
This example shows how enums in C# have underlying numeric values. First, we define an enum Color with named constants Red=1, Green=2, Blue=3. Then, we assign the variable c to Color.Green. To get the numeric value of c, we cast it to int, which gives 2. Finally, we print this numeric value. The execution table traces each step, showing how variables change. Beginners often wonder why casting is needed; it's because enums are not numbers directly but named constants. Changing enum values changes the numeric output accordingly.

Practice

(1/5)
1. What is the underlying numeric value of an enum member in C# if not explicitly set?
easy
A. It starts at 0 and increments by 1 for each member
B. It is always 1 for all members
C. It is a random number assigned by the compiler
D. It must be manually assigned for each member

Solution

  1. Step 1: Understand default enum numbering

    By default, C# assigns the first enum member the value 0.
  2. Step 2: Check subsequent members

    Each following member increments by 1 automatically unless explicitly set.
  3. Final Answer:

    It starts at 0 and increments by 1 for each member -> Option A
  4. Quick Check:

    Default enum values start at 0 and increment [OK]
Hint: Remember enums start at zero unless you say otherwise [OK]
Common Mistakes:
  • Thinking enums start at 1 by default
  • Assuming enum values are random
  • Believing manual assignment is always required
2. Which of the following is the correct way to get the numeric value of an enum member Day.Monday in C#?
easy
A. int value = Convert.ToInt32(Day.Monday.ToString());
B. int value = Day.Monday.ToInt();
C. int value = Day.Monday.Value;
D. int value = (int)Day.Monday;

Solution

  1. Step 1: Recall casting enum to int

    In C#, you get the numeric value by casting the enum member to int using (int).
  2. Step 2: Check other options

    Enums have no ToInt() method or Value property; Convert.ToInt32(Day.Monday.ToString()) parses the name ("Monday") and throws FormatException.
  3. Final Answer:

    int value = (int)Day.Monday; -> Option D
  4. Quick Check:

    Cast enum to int with (int) [OK]
Hint: Cast enum to int with (int) to get its number [OK]
Common Mistakes:
  • Using non-existent methods like ToInt()
  • Trying to access a Value property
  • Converting enum.ToString() to int (parses name)
3. Given the enum:
enum Color { Red = 3, Green, Blue }

What is the output of this code?
Console.WriteLine((int)Color.Green);
medium
A. 4
B. 3
C. 1
D. 0

Solution

  1. Step 1: Identify explicit enum value

    Color.Red is set to 3 explicitly.
  2. Step 2: Understand implicit increment

    Color.Green is next, so it gets 4 (3 + 1).
  3. Final Answer:

    4 -> Option A
  4. Quick Check:

    Enum increments from explicit value [OK]
Hint: Next enum value increments by 1 from previous explicit value [OK]
Common Mistakes:
  • Assuming Green is 1 by default
  • Thinking Green equals Red's value
  • Forgetting implicit increment after explicit value
4. What is wrong with this code snippet?
enum Status { Active = 1, Inactive = 1, Pending = 2 }
medium
A. Enum members cannot have duplicate values
B. Nothing is wrong; duplicate values are allowed
C. Enum members must be strings, not numbers
D. Enum members must be assigned sequentially starting at 0

Solution

  1. Step 1: Check enum value rules

    C# allows enum members to share the same underlying numeric value.
  2. Step 2: Verify other options

    Enum members can be numbers, do not need sequential values, and duplicates are allowed.
  3. Final Answer:

    Nothing is wrong; duplicate values are allowed -> Option B
  4. Quick Check:

    Duplicate enum values are valid in C# [OK]
Hint: Duplicate enum values are allowed in C# [OK]
Common Mistakes:
  • Thinking duplicate enum values cause errors
  • Believing enums must start at 0 and be sequential
  • Confusing enum members with strings
5. You have this enum:
enum Level { Low = 1, Medium = 5, High = 10 }

Which code correctly checks if an integer variable score matches any enum value?
hard
A. bool isMatch = score >= (int)Level.Low && score <= (int)Level.High;
B. bool isMatch = (Level)score != null;
C. bool isMatch = Enum.IsDefined(typeof(Level), score);
D. bool isMatch = score == Level.Low || score == Level.Medium || score == Level.High;

Solution

  1. Step 1: Understand Enum.IsDefined usage

    Enum.IsDefined checks if a given integer matches any enum member's underlying value.
  2. Step 2: Analyze other options

    A checks range (includes invalid like 2); B casts int to enum (always succeeds) != null (always true); C direct int == enum comparisons do not compile without casting enum to int.
  3. Final Answer:

    bool isMatch = Enum.IsDefined(typeof(Level), score); -> Option C
  4. Quick Check:

    Use Enum.IsDefined to check valid enum values [OK]
Hint: Use Enum.IsDefined to check if int matches enum value [OK]
Common Mistakes:
  • Casting int to enum without validation
  • Checking range instead of exact enum values
  • Comparing int directly to enum members without casting
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard