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

Enum vs constants decision in C Sharp (C#) - Visual Side-by-Side Comparison

Choose your learning style10 modes available
Concept Flow - Enum vs constants decision
Start
↓
Need named values?
Yes↓
Are values related and fixed set?
No→Use constants
Yes↓
Use enum
↓
Use in code
↓
End
Decide if you need a fixed set of related named values (enum) or just separate constant values.
Execution Sample
C Sharp (C#)
enum Color { Red, Green, Blue }
const int MaxValue = 100;
Color c = Color.Green;
int max = MaxValue;
Shows defining an enum and a constant, then using them in variables.
Execution Table
StepActionEvaluationResult
1Define enum Color with Red=0, Green=1, Blue=2N/AEnum Color created
2Define constant MaxValue = 100N/AConstant MaxValue created
3Assign c = Color.GreenColor.Green value is 1c = 1
4Assign max = MaxValueMaxValue is 100max = 100
5Use c and max in codec is enum value 1, max is 100Values ready for use
💡 All definitions and assignments complete, program ready to use enum and constant values.
Variable Tracker
VariableStartAfter Step 3After Step 4Final
cundefined1 (Color.Green)11
maxundefinedundefined100100
Key Moments - 2 Insights
Why use enum instead of constants for related values?
Enums group related values under one type, making code clearer and safer, as shown in step 1 and 3 where Color.Green is a named value.
Can constants replace enums?
Constants can hold values but lack grouping and type safety. Step 2 shows a constant, but step 3 shows enum usage is clearer for related sets.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what value does variable 'c' hold after step 3?
A0
B2
C1
D100
💡 Hint
Check the 'Result' column in row for step 3 in execution_table.
At which step is the constant MaxValue assigned to variable 'max'?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'Action' column for assignment to 'max' in execution_table.
If you need a fixed set of related named values, which should you choose according to the concept flow?
AVariables
BEnum
CConstants
DMethods
💡 Hint
Refer to the decision path in concept_flow ASCII diagram.
Concept Snapshot
Enum vs Constants Decision:
- Use enum for fixed sets of related named values.
- Use constants for single fixed values.
- Enums improve code clarity and safety.
- Constants are simple but less structured.
- Choose based on grouping and usage needs.
Full Transcript
This visual execution shows how to decide between enums and constants in C#. First, check if you need named values. If yes, decide if they form a fixed related set. If yes, use enum; otherwise, use constants. The example defines an enum Color with three values and a constant MaxValue. Variables c and max are assigned these values. The execution table traces each step, showing how c gets the enum value 1 for Green, and max gets 100 from the constant. Variable tracker shows how c and max change after assignments. Key moments clarify why enums group related values and why constants alone may not be enough. The quiz tests understanding of values and decision steps. The snapshot summarizes when to use enums or constants for clearer, safer code.

Practice

(1/5)
1. Which of the following is the best reason to use an enum instead of constants in C#?
easy
A. To group related named values under one type for better readability
B. To store a single fixed value with a name
C. To improve performance by using fewer bytes
D. To allow values to change at runtime

Solution

  1. Step 1: Understand the purpose of enums

    Enums group related named values under one type, making code clearer and safer.
  2. Step 2: Compare with constants

    Constants hold single fixed values, not groups of related options.
  3. Final Answer:

    To group related named values under one type for better readability -> Option A
  4. Quick Check:

    Enums group related values = D [OK]
Hint: Enums group options; constants hold single fixed values [OK]
Common Mistakes:
  • Thinking constants can group multiple related values
  • Believing enums allow runtime value changes
  • Confusing performance benefits between enums and constants
2. Which of the following is the correct way to declare an enum in C#?
easy
A. enum Colors = [Red, Green, Blue];
B. const enum Colors = { Red, Green, Blue };
C. const Colors { Red, Green, Blue };
D. enum Colors { Red, Green, Blue };

Solution

  1. Step 1: Recall enum syntax in C#

    An enum is declared using the keyword enum followed by the name and braces with values separated by commas.
  2. Step 2: Check each option

    enum Colors { Red, Green, Blue }; matches the correct syntax. Options A, B, and D use invalid keywords or assignment syntax.
  3. Final Answer:

    enum Colors { Red, Green, Blue }; -> Option D
  4. Quick Check:

    Correct enum syntax = C [OK]
Hint: Use 'enum Name { ... }' without assignment or const [OK]
Common Mistakes:
  • Using 'const' keyword with enums
  • Assigning enum with '=' or using brackets []
  • Missing commas between enum values
3. Given the following code, what is the output?
enum Status { Pending = 1, Approved = 2, Rejected = 3 }
class Program {
    static void Main() {
        Status s = Status.Approved;
        Console.WriteLine((int)s);
    }
}
medium
A. Approved
B. 2
C. 1
D. Status.Approved

Solution

  1. Step 1: Understand enum underlying values

    The enum Status assigns Approved the integer value 2.
  2. Step 2: Casting enum to int

    Casting Status.Approved to int prints its underlying value, which is 2.
  3. Final Answer:

    2 -> Option B
  4. Quick Check:

    Enum cast to int = 2 [OK]
Hint: Casting enum to int prints its numeric value [OK]
Common Mistakes:
  • Printing enum name instead of int value
  • Assuming default enum values start at 0
  • Confusing enum member with string output
4. What is wrong with this code snippet?
const int MaxValue = 100;
enum Limits { Min = 0, Max = MaxValue }
medium
A. Enum members must be assigned constant expressions, but MaxValue is not allowed here
B. Constants cannot be declared outside methods
C. Enums cannot use constants as values
D. Nothing is wrong; this code compiles fine

Solution

  1. Step 1: Check enum member assignment rules

    Enum members must be assigned constant expressions known at compile time, but they cannot reference constants declared outside the enum or class scope.
  2. Step 2: Analyze constant usage in enum

    MaxValue is a const int declared outside the enum, which is not allowed as an enum member initializer.
  3. Final Answer:

    Enum members must be assigned constant expressions, but MaxValue is not allowed here -> Option A
  4. Quick Check:

    Enum members cannot reference external consts = A [OK]
Hint: Enum members must be assigned literals or consts declared within the enum or class [OK]
Common Mistakes:
  • Thinking constants declared outside the enum cannot be used
  • Believing constants cannot be declared as class fields (outside methods)
  • Confusing const fields with runtime variables
5. You need to represent the four seasons (Spring, Summer, Autumn, Winter) in your program. You also want to store the average temperature for each season as a fixed value. Which approach is best in C#?
hard
A. Use only constants for both seasons and temperatures
B. Use an enum with temperature values assigned to each season
C. Use an enum for seasons and separate constants for average temperatures
D. Use a class with static readonly fields for seasons and temperatures

Solution

  1. Step 1: Identify the nature of seasons and temperatures

    Seasons are a fixed set of named options, perfect for enums. Temperatures are fixed values but not options themselves.
  2. Step 2: Decide how to store temperatures

    Since temperatures are fixed numeric values related to seasons, constants are suitable to hold these values separately.
  3. Step 3: Evaluate other options

    Assigning temperatures directly to enum members is not ideal because enum values should be simple constants, not meaningful data like temperatures. Using a class with static fields is more complex than needed.
  4. Final Answer:

    Use an enum for seasons and separate constants for average temperatures -> Option C
  5. Quick Check:

    Enum for options + constants for fixed values = B [OK]
Hint: Use enums for sets, constants for related fixed values [OK]
Common Mistakes:
  • Assigning complex data to enum values
  • Using only constants for grouped options
  • Overcomplicating with classes when not needed
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