Skip to footer content
Iron Academy Logo
Learn C#

Understanding C# Set Flag Enum: Assigning Flags

Tim Corey
9m 57s

When working with permissions, configurations, or settings in C#, we often encounter situations where multiple values need to be stored in a single variable. Instead of using separate Boolean values for each setting, we can use Flag Enums. This method leverages binary operations to efficiently manage multiple options within a single integer value.

In this article, we will take a deeper look at how to set Flag Enums in C# based on Tim Corey’s video, Binary in C#: Assigning Flags in 10 Minutes or Less. Tim walks through how to assign multiple values using binary operations, making our code more efficient and readable. Let’s dive in.

Introduction to Flag Enums

Tim starts by explaining that this lesson is part of a series on understanding binary in C#. In this particular video, he focuses on how to assign flags using binary operations. Flags allow us to store multiple values in a single integer, making permission management much simpler and more efficient.

Assigning Multiple Flags Using Bitwise OR

Tim demonstrates how we can combine multiple permissions using the bitwise OR (|) operator. He creates an enumeration SystemPermissions that includes different permission levels such as SQL, Redis, and BuildSystem. Instead of using multiple variables, we can store all permissions in one value.

Example of assigning multiple permissions to a variable:

// Assume that SystemPermissions is an enum with values SQL, Redis, and BuildSystem
SystemPermissions TimPermissions = SystemPermissions.SQL | SystemPermissions.Redis;

// Print the current permissions - it will include both SQL and Redis
Console.WriteLine(TimPermissions);
// Assume that SystemPermissions is an enum with values SQL, Redis, and BuildSystem
SystemPermissions TimPermissions = SystemPermissions.SQL | SystemPermissions.Redis;

// Print the current permissions - it will include both SQL and Redis
Console.WriteLine(TimPermissions);

Displaying Binary Representation

To better understand what’s happening, Tim suggests converting the integer value to its binary form using Convert.ToString(value, 2). This allows us to visualize which bits are set:

// Convert the permissions to a binary string to see the set bits
Console.WriteLine(Convert.ToString((int)TimPermissions, 2));
// Convert the permissions to a binary string to see the set bits
Console.WriteLine(Convert.ToString((int)TimPermissions, 2));

This shows that different bit positions correspond to different permissions, demonstrating how we can store multiple settings in one integer.

Adding More Permissions Using OR-Equals

Tim explains that we can add more permissions dynamically using the |= operator:

// Add the BuildSystem permission to the existing permissions
TimPermissions |= SystemPermissions.BuildSystem;

// Print the updated permissions - it now includes SQL, Redis, and BuildSystem
Console.WriteLine(TimPermissions);
// Add the BuildSystem permission to the existing permissions
TimPermissions |= SystemPermissions.BuildSystem;

// Print the updated permissions - it now includes SQL, Redis, and BuildSystem
Console.WriteLine(TimPermissions);

Understanding the 32-bit Limit

Tim highlights an important consideration: the integer data type in C# has a 32-bit limit. This means that we can only store up to 32 unique flags in an int. If we need more than 32 flags, we should consider using a long (64-bit) or even multiple values. However, in most cases, 32 flags are more than enough.

Storing Flags in a Database

One of the biggest advantages of using flag enums is that they can be stored efficiently in a database. Instead of storing multiple Boolean columns, we can store a single integer value representing all permissions. Tim explains that this approach significantly reduces database complexity and storage requirements.

For example, if TimPermissions is stored as 37, it represents a combination of:

  • SQL (bit 1)
  • Redis (bit 3)
  • BuildSystem (bit 5)

When we retrieve this value from the database, we can easily decode it to determine the active permissions.

Conclusion

Tim wraps up by emphasizing the power of Flag Enums. Using binary operations, we can efficiently manage multiple states within a single integer. This technique improves performance, simplifies data storage, and makes our code more readable.

By following this approach, you can implement a clean, scalable permission management system using C# Flag Enums. Watch the full video to get better understanding.

Hero Worlddot related to Understanding C# Set Flag Enum: Assigning Flags
Hero Affiliate related to Understanding C# Set Flag Enum: Assigning Flags

Earn More by Sharing What You Love

Do you create content for developers working with .NET, C#, Java, Python, or Node.js? Turn your expertise into extra income!