0
0
C Sharp (C#)programming~5 mins

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

Choose your learning style9 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:<br>
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:<br>
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:<br>
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.