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

Enum declaration syntax in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is an enum in C#?
An enum (short for enumeration) is a special data type that lets you define a group of named constant values. It helps make code more readable by using names instead of numbers.
Click to reveal answer
beginner
How do you declare a simple enum in C#?
Use the enum keyword followed by the enum name and a list of named values inside curly braces. For example:
enum Days { Sunday, Monday, Tuesday }
Click to reveal answer
intermediate
Can you assign specific integer values to enum members in C#?
Yes, you can assign specific integer values to enum members by using the equals sign. For example:
enum Days { Sunday = 1, Monday = 2, Tuesday = 3 }
Click to reveal answer
intermediate
What is the default underlying type of an enum in C#?
The default underlying type of an enum in C# is int. This means each enum member is stored as an integer unless you specify a different type.
Click to reveal answer
advanced
How do you specify a different underlying type for an enum in C#?
You specify the underlying type by adding a colon and the type name after the enum name. For example:
enum Days : byte { Sunday, Monday, Tuesday }
This makes the enum use byte instead of int.
Click to reveal answer
Which keyword is used to declare an enum in C#?
Aenum
Bclass
Cstruct
Dinterface
What is the default underlying type of an enum in C#?
Abyte
Bstring
Cint
Dlong
How do you assign a specific integer value to an enum member?
AYou cannot assign specific values
BUse a colon after the member name
CUse parentheses after the member name
DUse an equals sign after the member name
Which of these is a valid enum declaration with a specified underlying type?
Aenum Colors { Red, Green, Blue }
Benum Colors : byte { Red, Green, Blue }
Cenum Colors = int { Red, Green, Blue }
Denum Colors (int) { Red, Green, Blue }
What will be the integer value of the first member if you don't assign values explicitly?
A0
B1
C-1
DUndefined
Explain how to declare an enum in C# and how to assign specific values to its members.
Think about the basic syntax and how to set numbers for each name.
You got /4 concepts.
    Describe the default underlying type of enums in C# and how to change it.
    Remember the colon syntax after the enum name.
    You got /3 concepts.

      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