Actual uses of bit flags in .NET framework
Asked Answered
A

6

10

Was looking at how enums can be used as bit flags by decorating them with the flags attribute and bitwize operators (see below).

Are there any places in the .NET framework that this pattern is used? I like this but want to see some more real life examples

[Flags]
enum Days2 : int
{

  None = 0x0,
  Sunday = 0x1,
  Monday = 0x2,
  Tuesday = 0x4,
  Wednesday = 0x8,
  Thursday = 0x10,
  Friday = 0x20,
  Saturday = 0x40
}

  Days2 meetingDays = Days2.Tuesday | Days2.Thursday;

  // Set an additional flag using bitwise OR.
  meetingDays = meetingDays | Days2.Friday;

  Console.WriteLine("Meeting days are {0}", meetingDays);
Archoplasm answered 10/8, 2010 at 12:20 Comment(0)
E
11

Yes - look at MethodBase.Attributes for example, saying whether a member is public, static etc.

FileAccess and FileOptions are file-based examples, too.

If you open reflector, find FlagsAttribute and then hit "Analyze" (Ctrl-R) and expand "used by" you'll see loads of types using it. It takes a while though :)

Embellish answered 10/8, 2010 at 12:24 Comment(1)
Doh! I knew there should be a way to find it in reflector, rather than handcrafting some code.Nashbar
Z
5

One of the most common [Flags] enums you'll see is the regex options enum. Here is an example:

Regex rxInsensitive = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b",
          RegexOptions.Compiled | RegexOptions.IgnoreCase);

More here: http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regexoptions.aspx

Years ago I wrote a tutorial on using bit enums in .Net, maybe this will help: http://www.johnsample.com/articles/BitwiseEnums.aspx

Zeppelin answered 10/8, 2010 at 12:27 Comment(0)
S
2

There are many places where the FlagsAttribute is used for enumrations in the BCL.

Here is one example - the BindingFlags parameter used in reflection code.

Strohbehn answered 10/8, 2010 at 12:24 Comment(0)
O
2

There are tons of places it's used, it's used in the .NET framework in file attributes, I've seen it used as security flags i.e.

enum Roles
{
    None = 0,
    User = 1,
    Admin = 2,
    Superman = 4
}

if (_user.Roles & Roles.Admin != Roles.None) { user-is-admin }
if (_user.Roles & Roles.Superman != Roles.None) { user-is-also-superman }

They're basically great for any scenario where you have a selection of possible attributes for something, if you have something that could be a selection of colors for example or a selection of ethnicitys or etc.

Another common place to see them is winforms dialogs have attributes made up of them, you can or them together for a messagebox to decide the selection of buttons you get like Ok | Cancel | Ignore etc.

Outcry answered 10/8, 2010 at 12:26 Comment(0)
N
2

There are plenty of examples in the mscorlib and System assemblies, although how many you'll encounter in everyday use is a trickier question.

For instance, System.Threading.ThreadState is a combination of the current thread status, and pending requests.

I actually dumped out all of the flag enums in System and mscorlib, just for giggles, and then went searching for interesting ones:

    Dim types = (From t In Reflection.Assembly.GetAssembly(GetType(Int32)).GetTypes() Select t).Concat( _
                (From t In Reflection.Assembly.GetAssembly(GetType(Uri)).GetTypes() Select t))

    For Each t As Type In types
        If t.IsEnum AndAlso (From att In t.GetCustomAttributes(True) Where TypeOf (att) Is FlagsAttribute).Any() Then
            Console.WriteLine("Flag Enum: {0}", t.ToString())
        End If
    Next
    Console.ReadLine()
Nashbar answered 10/8, 2010 at 12:33 Comment(0)
A
2

How about when setting font style options, such as :

this.Font = new Font(this.Font, FontStyle.Bold | FontStyle.Italic);
Anaplastic answered 10/8, 2010 at 12:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.