What is an Enum in C#?
๐ก Concept: Enum in C#
An enum (short for enumeration) in C# is a value type that lets you define a set of named constants. It's used to make code more readable and maintainable when working with related sets of values.
๐ Quick Intro
Enums map friendly names to constant values, typically integers. They improve code clarity when dealing with options, states, or fixed categories like days of the week or status codes.
๐ง Analogy
Think of an enum like a dropdown menu in a form. Instead of entering a free-text value like ""High"" or ""Low"", you select from a fixed list. Enums ensure that only predefined options are allowed, preventing invalid input.
๐ง Technical Explanation
- Enum is a value type derived from System.Enum.
- Each member of an enum maps to a numeric value starting from 0 by default.
- You can assign specific values to enum members.
- Enums can be explicitly typed as byte, int, short, etc.
- They support bitwise combinations with [Flags] attribute.
๐ฏ Use Cases
- โ Representing fixed sets of options like Status, Priority, Days, etc.
- โ Improving code readability and reducing magic numbers.
- โ Used in switch statements for decision branching.
- โ Helpful in modeling state machines or configuration settings.
๐ป Code Example
enum Priority {
Low,
Medium,
High
}
class Task {
public string Title;
public Priority Level;
public void Print() {
Console.WriteLine($""Task: {Title}, Priority: {Level}"");
}
}
Task t = new Task { Title = ""Fix Bug #1023"", Level = Priority.High };
t.Print(); // Output: Task: Fix Bug #1023, Priority: High

โ Interview Q&A
Q1: What is an enum in C#?
A: An enum is a value type representing a set of named constants.
Q2: Can an enum store string values?
A: No, enums store numeric values internally.
Q3: What is the default underlying type of enum?
A: It is int unless explicitly changed.
Q4: Can you assign values to enum members?
A: Yes, you can assign custom numeric values.
Q5: How do you get the string name from an enum value?
A: Use .ToString() method.
Q6: What is the [Flags] attribute used for?
A: It enables bitwise operations on enum values.
Q7: Are enums zero-based in C#?
A: Yes, unless otherwise specified.
Q8: Can enums be generic?
A: No, enums cannot be generic.
Q9: Can you use enums in switch statements?
A: Yes, enums are ideal for switch statements.
Q10: Can an enum inherit from another enum?
A: No, enums do not support inheritance.
๐ MCQs
Q1. What is the default underlying type of an enum?
- byte
- int
- string
- char
Q2. Which attribute allows bitwise operation on enum?
- [Flags]
- [Bitwise]
- [Binary]
- [Logic]
Q3. What does enum.ToString() return?
- Name of enum member
- Numeric value
- Class name
- Error
Q4. Can enums inherit from another enum?
- Yes
- No
- Only abstract enums
- Only if static
Q5. Can enum members have duplicate values?
- Yes
- No
- Only in [Flags]
- Only with strings
Q6. Can you use enums in switch statements?
- Yes
- No
- Only with int
- Only in methods
Q7. What is true about enums in C#?
- Enums are reference types
- Enums are classes
- Enums are value types
- Enums are generic
Q8. What is the starting value of an enum without explicit values?
- 0
- 1
- -1
- Null
Q9. Can an enum be declared inside a class?
- Yes
- No
- Only public enums
- Only static enums
Q10. Which namespace contains the Enum base class?
- System
- Microsoft.CSharp
- System.Type
- System.Base
๐ก Bonus Insight
Enums not only make your code cleaner but also reduce bugs caused by invalid inputs. They're frequently used in state management, UI logic, and configuration code. Combine them with dictionaries for readable mappings.
๐ PDF Download
Need a handy summary for your notes? Download this topic as a PDF!