What if a simple change could stop your code from mixing up important values?
Enum vs constants decision in C Sharp (C#) - When to Use Which
Imagine you have a program where you need to represent different user roles like Admin, Guest, and Member. You write separate constant values for each role manually.
Manually managing many constants can get confusing and error-prone. You might accidentally reuse values or mistype names, making your code hard to read and maintain.
Using enums groups related constants under a single type. This makes your code clearer, safer, and easier to update because the compiler helps catch mistakes.
const int Admin = 1; const int Guest = 2; const int Member = 3;
enum UserRole { Admin, Guest, Member }Enums let you write cleaner code that clearly shows the meaning of values and prevents accidental errors.
In a game, you can use enums to represent different character classes like Warrior, Mage, or Archer instead of scattered constants, making the code easier to understand and extend.
Manual constants are easy to mix up and hard to manage.
Enums group related values clearly and safely.
Using enums improves code readability and reduces bugs.
Practice
enum instead of constants in C#?Solution
Step 1: Understand the purpose of enums
Enums group related named values under one type, making code clearer and safer.Step 2: Compare with constants
Constants hold single fixed values, not groups of related options.Final Answer:
To group related named values under one type for better readability -> Option AQuick Check:
Enums group related values = D [OK]
- Thinking constants can group multiple related values
- Believing enums allow runtime value changes
- Confusing performance benefits between enums and constants
Solution
Step 1: Recall enum syntax in C#
An enum is declared using the keywordenumfollowed by the name and braces with values separated by commas.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.Final Answer:
enum Colors { Red, Green, Blue }; -> Option DQuick Check:
Correct enum syntax = C [OK]
- Using 'const' keyword with enums
- Assigning enum with '=' or using brackets []
- Missing commas between enum values
enum Status { Pending = 1, Approved = 2, Rejected = 3 }
class Program {
static void Main() {
Status s = Status.Approved;
Console.WriteLine((int)s);
}
}Solution
Step 1: Understand enum underlying values
The enumStatusassignsApprovedthe integer value 2.Step 2: Casting enum to int
CastingStatus.Approvedto int prints its underlying value, which is 2.Final Answer:
2 -> Option BQuick Check:
Enum cast to int = 2 [OK]
- Printing enum name instead of int value
- Assuming default enum values start at 0
- Confusing enum member with string output
const int MaxValue = 100;
enum Limits { Min = 0, Max = MaxValue }Solution
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.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.Final Answer:
Enum members must be assigned constant expressions, but MaxValue is not allowed here -> Option AQuick Check:
Enum members cannot reference external consts = A [OK]
- 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
Solution
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.Step 2: Decide how to store temperatures
Since temperatures are fixed numeric values related to seasons, constants are suitable to hold these values separately.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.Final Answer:
Use an enum for seasons and separate constants for average temperatures -> Option CQuick Check:
Enum for options + constants for fixed values = B [OK]
- Assigning complex data to enum values
- Using only constants for grouped options
- Overcomplicating with classes when not needed
Ready to prove it?
Pass the test and this pattern moves unknown
