📖 Scenario: Imagine you are building a simple app to manage user permissions. Each user can have multiple permissions like Read, Write, and Execute. You want to store these permissions efficiently and check them easily.
🎯 Goal: You will create a bitwise enum with the [Flags] attribute to represent user permissions. Then, you will combine permissions, check if a user has a specific permission, and display the result.
📋 What You'll Learn
Create a bitwise enum called Permissions with values Read = 1, Write = 2, and Execute = 4 and apply the [Flags] attribute.
Create a variable called userPermissions and assign it the combination of Read and Write permissions.
Write code to check if userPermissions includes the Write permission using a bitwise operation.
Print the userPermissions value and the result of the check.
💡 Why This Matters
🌍 Real World
Bitwise enums with the <code>[Flags]</code> attribute are used in software to efficiently store and check multiple options or permissions in one variable.
💼 Career
Understanding flags and bitwise operations is important for roles in software development, especially when working with system settings, permissions, or configuration flags.
Progress0 / 4 steps
1
Create the Permissions enum with Flags attribute
Create a public enum called Permissions with the [Flags] attribute. Add these exact entries: Read = 1, Write = 2, and Execute = 4.
C Sharp (C#)
Hint
The [Flags] attribute allows combining enum values with bitwise operations.
2
Assign combined permissions to a variable
Create a variable called userPermissions of type Permissions and assign it the combination of Permissions.Read and Permissions.Write using the bitwise OR operator |.
C Sharp (C#)
Hint
Use | to combine enum values.
3
Check if userPermissions includes Write permission
Write a boolean variable called hasWrite that checks if userPermissions includes Permissions.Write using the bitwise AND operator & and compare the result to Permissions.Write.
C Sharp (C#)
Hint
Use & to check if a specific flag is set.
4
Print the userPermissions and hasWrite values
Write two Console.WriteLine statements: one to print userPermissions and one to print hasWrite.
C Sharp (C#)
Hint
The first line should print combined permissions as text, the second line prints True if Write is included.
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?