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

Underlying numeric values in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What are underlying numeric values in C# enums?
Underlying numeric values are the integer values automatically or explicitly assigned to each enum member. By default, they start at 0 and increase by 1 for each member.
Click to reveal answer
beginner
How can you specify a custom underlying numeric value for an enum member in C#?
You assign a value directly to the enum member, for example: enum Days { Sunday = 1, Monday = 2 }.
Click to reveal answer
beginner
What is the default underlying type of an enum in C#?
The default underlying type of an enum in C# is int.
Click to reveal answer
intermediate
Can you change the underlying type of an enum in C#? If yes, how?
Yes, you can specify a different integral type like byte, short, long by declaring it after the enum name, e.g., enum Colors : byte { Red, Green }.
Click to reveal answer
beginner
How do you get the underlying numeric value of an enum member in C#?
You can cast the enum member to its underlying type, for example: int value = (int)Days.Monday;.
Click to reveal answer
What is the default underlying numeric value of the first enum member if not specified?
A0
B1
C-1
DDepends on the enum
How do you specify an enum with an underlying type of byte?
Aenum Colors(byte) { Red, Green }
Benum Colors : byte { Red, Green }
Cenum Colors { Red = byte, Green = byte }
Denum Colors { byte Red, byte Green }
What happens if you assign the same numeric value to two enum members?
AIt causes a compile error
BThe second member gets incremented automatically
CBoth members share the same underlying value
DThe enum becomes invalid
How do you retrieve the numeric value of an enum member in C#?
AUse a cast like (int)EnumMember
BUse Enum.GetValue() method
CUse Enum.ToString()
DUse Enum.Parse()
Which of these is NOT a valid underlying type for an enum in C#?
Aint
Bbyte
Clong
Dstring
Explain what underlying numeric values are in C# enums and how they are assigned.
Think about how enum members get their numbers.
You got /3 concepts.
    Describe how to change the underlying type of an enum and why you might want to do that.
    Look at the enum declaration syntax.
    You got /3 concepts.