What is the purpose of the [Flags] attribute in C# enums?
The [Flags] attribute allows an enum to represent a combination of values using bitwise operations, making it easier to work with sets of options.
Click to reveal answer
beginner
How do you define an enum with bitwise flags in C#?
Define enum members with values as powers of two (1, 2, 4, 8, etc.) so each bit represents a unique flag. Example: [Flags] enum MyFlags { None = 0, Read = 1, Write = 2, Execute = 4 }
Click to reveal answer
beginner
How can you check if a specific flag is set in a bitwise enum variable?
Use the bitwise AND operator (&) to check if the flag is set. For example, (myFlags & MyFlags.Read) == MyFlags.Read means the Read flag is set.
Click to reveal answer
beginner
What does combining flags with the bitwise OR operator (|) do?
It creates a new value that contains all the flags combined. For example, MyFlags.Read | MyFlags.Write combines both Read and Write flags.
Click to reveal answer
beginner
Why should enum values for flags be powers of two?
Because powers of two set unique bits in binary, allowing flags to be combined and checked without overlap or confusion.
Click to reveal answer
What does the [Flags] attribute do in a C# enum?
AAllows combining enum values using bitwise operations
BMakes enum values readonly
CChanges enum to string values
DPrevents enum from being used in switch statements
✗ Incorrect
The [Flags] attribute enables combining enum values using bitwise operations to represent multiple options.
Which of these is a correct value for a bitwise flag enum member?
A3
B5
C8
D7
✗ Incorrect
8 is a power of two (2^3), suitable for a unique bit flag. Values like 3, 5, 7 are combinations, not single flags.
How do you check if the Write flag is set in a variable 'flags' of a bitwise enum?
Aflags + Write
Bflags == Write
Cflags | Write
D(flags & Write) == Write
✗ Incorrect
Using bitwise AND (&) and comparing to the flag checks if that flag is set.
What is the result of combining Read (1) and Execute (4) flags using bitwise OR?
A7
B5
C3
D0
✗ Incorrect
1 | 4 equals 5 in binary, combining Read and Execute flags.
Why is it important to assign powers of two to enum flags?
ATo allow unique bits for each flag
BTo make enums smaller in memory
CTo sort enum values alphabetically
DTo prevent enum from being changed
✗ Incorrect
Powers of two ensure each flag uses a unique bit, enabling proper combination and checking.
Explain how the [Flags] attribute and bitwise enums work together in C#.
Think about how bits represent options and how you combine or check them.
You got /4 concepts.
Describe how to define and use a bitwise enum to represent multiple permissions like Read, Write, and Execute.
Imagine permissions as switches you can turn on or off.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the [Flags] attribute in C# enums?
easy
A. To allow combining multiple enum values using bitwise operations
B. To make enum values readonly
C. To automatically assign sequential integer values
D. To restrict enum values to a single choice only
Solution
Step 1: Understand the role of [Flags]
The [Flags] attribute marks an enum to be treated as a set of bit fields, allowing multiple values to be combined.
Step 2: Compare with other options
Options B, C, and D describe unrelated enum behaviors. Only To allow combining multiple enum values using bitwise operations correctly describes the bitwise combination feature.
Final Answer:
To allow combining multiple enum values using bitwise operations -> Option A
Quick Check:
[Flags] enables bitwise combination [OK]
Hint: Remember: [Flags] means combine enum values with bits [OK]
Common Mistakes:
Thinking [Flags] restricts enum to one value
Confusing [Flags] with readonly or sequential enums
Assuming [Flags] changes enum underlying type
2. Which of the following is the correct way to define a bitwise enum with the [Flags] attribute?
easy
A. [Flags]
public enum Colors { Red = 0, Green = 1, Blue = 2 }
B. [Flags]
public enum Colors { Red = 1, Green = 2, Blue = 4 }
C. public enum Colors { Red = 1, Green = 2, Blue = 3 }
D. [Flags]
public enum Colors { Red = 1, Green = 1, Blue = 2 }
Solution
Step 1: Check enum values for bitwise uniqueness
Bitwise enums require values as powers of two (1, 2, 4, 8...) to avoid overlap.
Step 2: Validate each option
[Flags]
public enum Colors { Red = 1, Green = 2, Blue = 4 } uses 1, 2, 4 which are correct powers of two. [Flags]
public enum Colors { Red = 0, Green = 1, Blue = 2 } starts with 0 which is valid for None but not typical for flags representing colors. public enum Colors { Red = 1, Green = 2, Blue = 3 } uses 3 which overlaps bits. [Flags]
public enum Colors { Red = 1, Green = 1, Blue = 2 } repeats 1 twice, causing overlap.
Final Answer:
[Flags]
public enum Colors { Red = 1, Green = 2, Blue = 4 } -> Option B
Quick Check:
Enum values must be powers of two [OK]
Hint: Use powers of two for flags enum values [OK]
Common Mistakes:
Assigning zero to a flag value
Using overlapping values like 3 or repeated numbers
Forgetting the [Flags] attribute
3. Given the enum and code below, what is the output?