Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 32975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T13:50:52+00:00 2026-05-10T13:50:52+00:00

From time to time I see an enum like the following: [Flags] public enum

  • 0

From time to time I see an enum like the following:

[Flags] public enum Options  {     None    = 0,     Option1 = 1,     Option2 = 2,     Option3 = 4,     Option4 = 8 } 

I don’t understand what exactly the [Flags] attribute does.

Anyone have a good explanation or example they could post?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T13:50:53+00:00Added an answer on May 10, 2026 at 1:50 pm

    The [Flags] attribute should be used whenever the enumerable represents a collection of possible values, rather than a single value. Such collections are often used with bitwise operators, for example:

    var allowedColors = MyColor.Red | MyColor.Green | MyColor.Blue; 

    Note that the [Flags] attribute doesn’t enable this by itself – all it does is allow a nice representation by the .ToString() method:

    enum Suits { Spades = 1, Clubs = 2, Diamonds = 4, Hearts = 8 } [Flags] enum SuitsFlags { Spades = 1, Clubs = 2, Diamonds = 4, Hearts = 8 }  ...  var str1 = (Suits.Spades | Suits.Diamonds).ToString();            // '5' var str2 = (SuitsFlags.Spades | SuitsFlags.Diamonds).ToString();            // 'Spades, Diamonds' 

    It is also important to note that [Flags] does not automatically make the enum values powers of two. If you omit the numeric values, the enum will not work as one might expect in bitwise operations, because by default the values start with 0 and increment.

    Incorrect declaration:

    [Flags] public enum MyColors {     Yellow,  // 0     Green,   // 1     Red,     // 2     Blue     // 3 } 

    The values, if declared this way, will be Yellow = 0, Green = 1, Red = 2, Blue = 3. This will render it useless as flags.

    Here’s an example of a correct declaration:

    [Flags] public enum MyColors {     Yellow = 1,     Green = 2,     Red = 4,     Blue = 8 } 

    To retrieve the distinct values in your property, one can do this:

    if (myProperties.AllowedColors.HasFlag(MyColor.Yellow)) {     // Yellow is allowed... } 

    or prior to .NET 4:

    if((myProperties.AllowedColors & MyColor.Yellow) == MyColor.Yellow) {     // Yellow is allowed... }  if((myProperties.AllowedColors & MyColor.Green) == MyColor.Green) {     // Green is allowed... }     

    Under the covers

    This works because you used powers of two in your enumeration. Under the covers, your enumeration values look like this in binary ones and zeros:

     Yellow: 00000001  Green:  00000010  Red:    00000100  Blue:   00001000 

    Similarly, after you’ve set your property AllowedColors to Red, Green and Blue using the binary bitwise OR | operator, AllowedColors looks like this:

    myProperties.AllowedColors: 00001110 

    So when you retrieve the value you are actually performing bitwise AND & on the values:

    myProperties.AllowedColors: 00001110              MyColor.Green: 00000010              -----------------------                             00000010 // Hey, this is the same as MyColor.Green! 

    The None = 0 value

    And regarding the use of 0 in your enumeration, quoting from MSDN:

    [Flags] public enum MyColors {     None = 0,     .... } 

    Use None as the name of the flag enumerated constant whose value is zero. You cannot use the None enumerated constant in a bitwise AND operation to test for a flag because the result is always zero. However, you can perform a logical, not a bitwise, comparison between the numeric value and the None enumerated constant to determine whether any bits in the numeric value are set.

    You can find more info about the flags attribute and its usage at msdn and designing flags at msdn

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 68k
  • Answers 68k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Form.ActiveControl may be what you want. May 11, 2026 at 12:09 pm
  • added an answer The following code does the job: var icon = new… May 11, 2026 at 12:09 pm
  • added an answer You need to make a modelbinder and register it. This… May 11, 2026 at 12:09 pm

Related Questions

From time to time I see an enum like the following: [Flags] public enum
I see this from time to time and want to know what it is.
From time to time I get a System.Threading.ThreadStateException when attempting to restart a thread.
From time to time I read that Fortran is or can be faster then
From time to time I browse the web and look for interesting algorithms and
I make a lot of web applications and from time to time I need
I am an advocate of ORM-solutions and from time to time I am giving
From time to time, I need to dump USB traffic under Windows, mostly to
From time to time, I run into communications issue with other programmers, when we
From time to time, a file that I'm interested in is modified by some

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.