0
0
C Sharp (C#)programming~10 mins

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

Choose your learning style9 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.