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!

๐Ÿ” Navigation

Learn More About C# ๐Ÿ“š

1. What is C#? ๐Ÿ‘‰ Explained
2. Main Features of C# ๐Ÿ‘‰ Explained
3. Difference Between C# and Java ๐Ÿ‘‰ Explained
4. Common Language Runtime (CLR) in C# ๐Ÿ‘‰ Explained
5. Common Type System (CTS) in C# ๐Ÿ‘‰ Explained
6. Common Language Specification (CLS) in C# ๐Ÿ‘‰ Explained
7. Value Types vs Reference Types in C# ๐Ÿ‘‰ Explained
8. What is a Namespace in C#? ๐Ÿ‘‰ Explained
9. Purpose of the 'using' Keyword in C# ๐Ÿ‘‰ Explained
10. Different Data Types in C# ๐Ÿ‘‰ Explained
11. Difference Between int and Int32 in C# ๐Ÿ‘‰ Explained
12. Difference Between float, double, and decimal in C# ๐Ÿ‘‰ Explained
13. What is the Default Value of a Boolean in C#? ๐Ÿ‘‰ Explained
14. What is Boxing and Unboxing in C# ๐Ÿ‘‰ Explained
15. What are the Different Types of Operators in C# ๐Ÿ‘‰ Explained
16. Difference Between Equals and == in C# ๐Ÿ‘‰ Explained
17. What is the Null-Coalescing Operator ?? in C# ๐Ÿ‘‰ Explained
18. What is the Ternary Operator in C# ๐Ÿ‘‰ Explained
19. How Does the Switch Statement Work in C# ๐Ÿ‘‰ Explained
20. What is Object-Oriented Programming in C# ๐Ÿ‘‰ Explained
21. What are the Four Pillars of OOP in C# ๐Ÿ‘‰ Explained
22. What is Encapsulation in C# ๐Ÿ‘‰ Explained
23. What is Inheritance in C# ๐Ÿ‘‰ Explained
24. What is Polymorphism in C# ๐Ÿ‘‰ Explained
25. What is Abstraction in C# ๐Ÿ‘‰ Explained
26. What is an Abstract Class in C# ๐Ÿ‘‰ Explained
27. What is an Interface in C# ๐Ÿ‘‰ Explained
28. Can a Class Implement Multiple Interfaces in C#? ๐Ÿ‘‰ Explained
29. Difference Between Abstract Class and Interface in C# ๐Ÿ‘‰ Explained
30. How Do You Create a Class in C#? ๐Ÿ‘‰ Explained
31. What is a Constructor in C# ๐Ÿ‘‰ Explained
32. What Are the Types of Constructors in C# ๐Ÿ‘‰ Explained
33. What is a Static Constructor in C# ๐Ÿ‘‰ Explained
34. Difference Between Static and Non-Static Members in C# ๐Ÿ‘‰ Explained
35. What is the Use of 'this' Keyword in C# ๐Ÿ‘‰ Explained
36. What is a Destructor in C# ๐Ÿ‘‰ Explained
37. What is Object Initializer Syntax in C# ๐Ÿ‘‰ Explained
38. What is the Difference Between Field and Property in C# ๐Ÿ‘‰ Explained
Share:

Tags:


Feedback Modal Popup