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

Enum declaration syntax in C Sharp (C#) - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Enum declaration syntax
Start Enum Declaration
↓
Write 'enum' keyword
↓
Name the Enum
↓
Open curly brace '{'
↓
List enum members
↓
Close curly brace '}'
↓
Enum declared successfully
This flow shows the steps to declare an enum: start with the keyword, name it, list members inside braces, then finish.
Execution Sample
C Sharp (C#)
enum Days {
    Sunday,
    Monday,
    Tuesday
}
Declares an enum named Days with three members: Sunday, Monday, and Tuesday.
Execution Table
StepCode PartActionResult
1enum DaysRecognize enum keyword and namePrepare to declare enum named Days
2{Open enum bodyStart listing members
3Sunday,Add member SundaySunday assigned value 0 by default
4Monday,Add member MondayMonday assigned value 1 by default
5TuesdayAdd member TuesdayTuesday assigned value 2 by default
6}Close enum bodyEnum Days declared with 3 members
💡 All enum members listed and enum declaration closed with '}'
Variable Tracker
VariableStartAfter 1After 2After 3Final
Days.Sundayundefined0000
Days.Mondayundefinedundefined111
Days.Tuesdayundefinedundefinedundefined22
Key Moments - 3 Insights
Why does Sunday get the value 0 automatically?
By default, enum members start at 0 and increase by 1 for each member, as shown in execution_table rows 3 to 5.
What happens if I forget the closing brace '}'?
The enum declaration is incomplete and will cause a syntax error; execution_table row 6 shows the importance of closing the enum.
Can enum members have explicit values?
Yes, but in this example, values are assigned automatically starting at 0, as seen in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what value is assigned to Monday?
A0
B2
C1
Dundefined
💡 Hint
Check execution_table row 4 where Monday is assigned value 1.
At which step does the enum declaration close?
AStep 6
BStep 4
CStep 3
DStep 2
💡 Hint
Look at execution_table row 6 where '}' closes the enum.
If you add a new member after Tuesday, what value will it have by default?
A2
B3
C0
DUndefined
💡 Hint
See variable_tracker: values increment by 1 for each new member.
Concept Snapshot
Enum declaration syntax in C#:
enum EnumName {
  Member1, // default 0
  Member2, // default 1
  Member3  // default 2
}
Members auto-increment starting at 0 unless assigned explicitly.
Full Transcript
This visual execution shows how to declare an enum in C#. First, write the keyword 'enum' followed by the enum name. Then open curly braces and list the members separated by commas. Each member gets an automatic integer value starting at 0 and increasing by 1. Finally, close the braces to complete the declaration. The execution table traces each step, showing how members get assigned values. The variable tracker shows the values of each member after each step. Key moments clarify common confusions like default values and the need for closing braces. The quiz tests understanding of member values and declaration steps.

Practice

(1/5)
1. What is the purpose of an enum in C#?
easy
A. To group related named constants together
B. To create a new class type
C. To define a method
D. To declare a variable

Solution

  1. Step 1: Understand enum concept

    An enum groups related named constants to make code clearer.
  2. Step 2: Compare with other options

    Enums are not classes, methods, or variables but a special type for constants.
  3. Final Answer:

    To group related named constants together -> Option A
  4. Quick Check:

    Enum = group constants [OK]
Hint: Enums group named constants, not variables or methods [OK]
Common Mistakes:
  • Thinking enum is a class
  • Confusing enum with method or variable
  • Assuming enum holds variable data
2. Which of the following is the correct syntax to declare an enum named Days with members Monday and Tuesday?
easy
A. enum Days: Monday, Tuesday;
B. enum Days = { Monday, Tuesday };
C. enum Days { Monday, Tuesday };
D. enum Days (Monday, Tuesday);

Solution

  1. Step 1: Recall enum declaration syntax

    Enums use curly braces with members separated by commas, no equals or parentheses.
  2. Step 2: Check each option

    enum Days { Monday, Tuesday }; matches correct syntax: enum Days { Monday, Tuesday };.
  3. Final Answer:

    enum Days { Monday, Tuesday }; -> Option C
  4. Quick Check:

    Enum syntax uses braces and commas [OK]
Hint: Use braces and commas for enum members [OK]
Common Mistakes:
  • Using equals sign (=) after enum name
  • Using parentheses instead of braces
  • Using colon (:) instead of opening brace
3. What is the output of this code?
enum Color { Red, Green, Blue };
Console.WriteLine((int)Color.Green);
medium
A. 0
B. 1
C. 2
D. Green

Solution

  1. Step 1: Understand default enum values

    Enum members start at 0 by default: Red=0, Green=1, Blue=2.
  2. Step 2: Cast enum member to int

    Printing (int)Color.Green outputs its numeric value 1.
  3. Final Answer:

    1 -> Option B
  4. Quick Check:

    Green = 1 by default [OK]
Hint: Enum members start at 0 unless assigned [OK]
Common Mistakes:
  • Assuming first member starts at 1
  • Printing enum name instead of int value
  • Confusing enum member order
4. Identify the error in this enum declaration:
enum Status
{
  Started = 1,
  InProgress = 2,
  Completed = "Done"
};
medium
A. Enum members must be separated by semicolons
B. Missing semicolon after enum declaration
C. Enum name must be lowercase
D. Enum members cannot have string values

Solution

  1. Step 1: Check enum member value types

    Enum members must be integral types (int, byte, etc.), not strings.
  2. Step 2: Analyze the given code

    "Completed = \"Done\"" assigns a string, which is invalid.
  3. Final Answer:

    Enum members cannot have string values -> Option D
  4. Quick Check:

    Enum values must be integers [OK]
Hint: Enum values must be integers, not strings [OK]
Common Mistakes:
  • Assigning string values to enum members
  • Thinking semicolon is required after enum
  • Assuming enum names must be lowercase
5. How can you declare an enum Priority with members Low=1, Medium=5, and High=10 so that the values are not sequential?
hard
A. enum Priority { Low = 1, Medium = 5, High = 10 };
B. enum Priority { Low, Medium, High }
C. enum Priority { Low = 1; Medium = 5; High = 10 }
D. enum Priority = { Low = 1, Medium = 5, High = 10 }

Solution

  1. Step 1: Assign explicit values to enum members

    To set custom values, assign each member an integer with = and commas between.
  2. Step 2: Check syntax correctness

    enum Priority { Low = 1, Medium = 5, High = 10 }; uses correct syntax with commas and explicit values.
  3. Final Answer:

    enum Priority { Low = 1, Medium = 5, High = 10 }; -> Option A
  4. Quick Check:

    Use = and commas for custom enum values [OK]
Hint: Assign values with = and separate members by commas [OK]
Common Mistakes:
  • Using semicolons instead of commas
  • Omitting explicit values for custom numbers
  • Using equals sign after enum name
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