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!

Tim Corey
8m 32s
C# Flags are a powerful yet often overlooked feature that allows developers to manage permissions and options using bitwise operations. In his video "Binary in C#: Flags in 10 minutes or less," Tim Corey provides a clear and concise explanation of how flags work and how they can be leveraged effectively in C#. In this article, we will break down Tim's explanations and insights from the video to gain a better understanding of C# Flags.
Tim starts by introducing the concept of Flags. He explains that Flags allow us to assign multiple values to a single variable, making them useful for managing permissions in an application. To illustrate this, Tim creates an enum called SystemPermissions that includes various permissions like SQL, WebServer, Redis, Azure, GitHub, and BuildSystem.
Tim highlights that without Flags, a single variable can hold only one permission at a time. For example, if we try to assign SystemPermissions.GitHub to a user, we cannot also assign SystemPermissions.BuildSystem unless we use an array, which becomes cumbersome to manage.
At this point, Tim demonstrates the limitations of traditional enumeration assignments. If we assign permissions one at a time, checking them later requires iterating through an array, which is inefficient and not scalable. This leads to an "ugly" implementation, as Tim describes it, where managing multiple permissions becomes a hassle.
To solve this issue, Tim introduces the [Flags] attribute, which allows an enum to be treated as a bit field. He explains that each permission is assigned a unique binary value using bitwise shifting.
Instead of assigning incremental numbers (0, 1, 2, 3, etc.), Tim assigns values using powers of two:
[Flags]
enum SystemPermissions
{
None = 0,
SQL = 1 << 0, // 00000001
WebServer = 1 << 1, // 00000010
Redis = 1 << 2, // 00000100
Azure = 1 << 3, // 00001000
GitHub = 1 << 4, // 00010000
BuildSystem = 1 << 5 // 00100000
}<Flags>
Enum SystemPermissions
None = 0
SQL = 1 << 0 ' 00000001
WebServer = 1 << 1 ' 00000010
Redis = 1 << 2 ' 00000100
Azure = 1 << 3 ' 00001000
GitHub = 1 << 4 ' 00010000
BuildSystem = 1 << 5 ' 00100000
End EnumTim explains that shifting the bits ensures that each permission has a unique value without overlapping, allowing for the efficient combination of multiple permissions.
Tim then demonstrates how to assign and combine permissions using bitwise operations. He explains that we can assign multiple permissions to a user by using the bitwise OR (|) operator:
SystemPermissions timsPermissions = SystemPermissions.SQL | SystemPermissions.Redis | SystemPermissions.GitHub;Dim timsPermissions As SystemPermissions = SystemPermissions.SQL Or SystemPermissions.Redis Or SystemPermissions.GitHubThis assigns SQL, Redis, and GitHub permissions to timsPermissions, storing them in a single variable using bitwise operations.
One of the key benefits of Flags is the ability to check if a specific permission is set using the bitwise AND (&) operator. Tim demonstrates how to verify if a user has a particular permission:
if ((timsPermissions & SystemPermissions.SQL) == SystemPermissions.SQL)
{
Console.WriteLine("Tim has SQL access.");
}If (timsPermissions And SystemPermissions.SQL) = SystemPermissions.SQL Then
Console.WriteLine("Tim has SQL access.")
End IfTim emphasizes that this approach is much more efficient than looping through an array, as it allows for quick bitwise comparisons.
Tim also covers how to remove a permission using the bitwise AND and NOT (~) operator. If we need to revoke SQL permission from timsPermissions, we do it like this:
timsPermissions &= ~SystemPermissions.SQL;timsPermissions = timsPermissions And Not SystemPermissions.SQLThis effectively removes the SQL permission while keeping the other permissions intact.
To visualize how Flags work, Tim suggests converting the enum value to its binary representation. He uses a helper method to display the binary value of a permission:
Console.WriteLine(Convert.ToString((int)timsPermissions, 2));Console.WriteLine(Convert.ToString(CInt(timsPermissions), 2))Tim explains that this helps in understanding how individual bits are set and manipulated within the enum.
Tim concludes by emphasizing why Flags are useful in C#:
He also points out that Flags are commonly used in scenarios like file permissions, user roles, and application settings.
Throughout the video, Tim Corey provides a clear and practical demonstration of how C# Flags work. By using the [Flags] attribute and bitwise operations, we can efficiently manage multiple permissions without needing complex data structures. If you're working with permissions or settings that require multiple values, Flags are an excellent tool to simplify your implementation.
For a hands-on understanding, check out Tim Corey's video, where he explains these concepts step by step. Flags may seem tricky at first, but once you understand their binary nature, they become an incredibly useful feature in C# development.
Do you create content for developers working with .NET, C#, Java, Python, or Node.js? Turn your expertise into extra income!
Join our newsletter, you’ll get exclusive access on article updates. We value your privacy