SlideShare une entreprise Scribd logo
1  sur  5
Ending the Great Debate on Enum Flags
By virusstorm | 4 Jun 2012 | Article
C#2.0C#3.0.NETDevBeginnerC#4.0
This article explains how enumeration and flags work in C#.NET and how to properly use
them.

See Also


More like this
More by this author


Article Browse Code Stats Revisions (4) Alternatives

      23


       Download demo project - 10.8 KB

Introduction

Everyone learns about enumeration when they start developing in C#.NET. It is a very
simply structure that can be used to solve a wide range of problems. One of the best uses
for enumeration is using them as a flag. In the .NET Framework, there is a handy attribute
you specify on your enumeration called Flags and now your enumeration will work as a
flag. Or so you thought.

The reality is there is work you need to do to make the enumeration to function like a flag. I
have seen experienced .NET developers get caught up working under the assumption
that Flags handles everything for you. I personally think the reason for this is due to the
documentation surrounding enumeration and flags. It seems to me the MSDN
documentation on enumeration fails to explain how enumeration and flags work which
causes a misunderstanding at implementation time.

In this article, I will explain what an enumeration is, what are flags, and how to make
enumeration operate like a flag.

Background

A few months ago (from when this article was written), I was trying to recall a technique
(which I will go over in this article) for setting up enumeration as flags. So I fired up my
trusty web browser and brought up Google and searched for "enum flags". You get a little
over two million results back and naturally, the first few are MSDN documentation pages. As
I was hunting for the article that had the technique I wanted, I became increasingly
frustrated with the mixed content and descriptions as to how enumeration and flags work. I
got so frustrated between the authors and the comments people were leaving, I decided to
set out and write an 'end all be all' solution to this problem.

Also, this is my first article, so be kind when you leave comments and feedback.
What is enumeration?

Enumeration is basically a list of possible values for a variable. The easiest way to apply
them is to think of the days of the week. Typically, Sunday is considered to be the first day
of the week and Saturday as the last day of the week. So if we assign a numerical value to
the days of the week, you get the following: Sunday = 1, Monday = 2, Tuesday = 3,
Wednesday = 4, Thursday = 5, Friday = 6, and Saturday = 7. So in our program, we can
use these numerical values for the day of the week as opposed to the string value. This
removes issues related with string comparison (case issues, which space, special characters,
etc.), and it also allows us to save memory and processing time. I know in today's world,
memory usage and CPU usage are not a huge issue versus twenty years ago, but that
doesn't give us the excuse to be lazy or design poorly.

In the C#.NET world, we use the keyword enum to define an enumeration. If we use the day
of the week example from above, we would see something like this:

                                                                               Collapse | Copy Code

enum DayOfTheWeek
{
    Sunday = 1,
    Monday = 2,
    Tuesday = 3,
    Wednesday = 4,
    Thursday = 5,
    Friday = 6,
    Saturday = 7,
}


The MSDN reference describes more ways and styles of defining an enumeration. I would
suggest checking out theenum (C# Reference) for more details if you are new to
enumeration.

What are flags?

The idea of flags is to take an enumeration value and allow it hold multiple values. If we
continue with our days of the week example, think of a scheduling application. You want to
set up a meeting for Monday, Wednesday, and Friday. The way we previously defined our
enumeration, we would need seven DayOfTheWeek variables defined or create a list
of DayOfTheWeek in order to store the days a meeting could be scheduled.

If we change the way we defined our enumeration to support the use of flags, we could
avoid adding seven variables or adding the overhead of a list. This is where life gets
confusing. A lot of people believe that simply adding theFlags attribute to the enumeration
will take care of this. That is not the case. To properly implement an enumeration as a flag,
you need to have the values increasing by a power of two. So if we define our enumeration
to do this, we would get the following:

                                                                               Collapse | Copy Code

[Flags]
enum DaysOfTheWeek
{
    Sunday = 1,
    Monday = 2,
Tuesday = 4,
    Wednesday = 8,
    Thursday = 16,
    Friday = 32,
    Saturday = 64
}


The Why and the How

So it is at this point, a lot of people get confused. Let us start off by understanding at a
binary level, what is going on.

Day of the Week Decimal Value Binary Value
     Sunday                 1            0000001
     Monday                 2            0000010
     Tuesday                4            0000100
    Wednesday               8            0001000
    Thursday               16            0010000
      Friday               32            0100000
    Saturday               64            1000000

What you see here, at a binary level, is a left shift of bits. This is a critical component to
flags and how we work with them.

Now let us take a look at how we assign values to them. Consider the following code
snippet:

                                                                                    Collapse | Copy Code

DaysOfTheWeek daysOfTheWeek;
daysOfTheWeek = DaysOfTheWeek.Monday | DaysOfTheWeek.Wednesday | DaysOfTheWeek.Friday;


What we have just done here is assigned Monday, Wednesday, and Friday to the
variable daysOfTheWeek. We did this by using an OR operator. Let us look at the binary
level again and see what happened.

Monday         0 0 0 0 0 1 0
               | | | | | | |
Wednesday 0 0 0 1 0 0 0
               | | | | | | |
Friday         0 1 0 0 0 0 0


Result         0 1 0 1 0 1 0
Take note of the result. Remember that the single pipe, "|", in C# is a logic operator, not
conditional. This means at a binary level, we OR each position of the binary values. Boolean
logic tells us in an OR situation the result is true when at least one condition is true;
otherwise the result is false. What you should begin to see now is that we are using each
position of the binary value as a switch or flag to indicate when a day of the week has been
selected.

To test to see if our result value has a specific flag set, we would use the following syntax:

                                                                                  Collapse | Copy Code

if((daysOfTheWeek & DaysOfTheWeek.Monday) == DaysOfTheWeek.Monday)
{
    // Contains Monday!
}
else
{
    // DOES NOT Contain Monday!
}


Before we did a logical OR to combine the flags to store them into a single variable. Now we
use the logical AND operator, "&", to test for the value. Let us look at the binary level to see
what happened.

daysOfTheWeek 0 1 0 1 0 1 0
                   & & & & & & &
Monday             0 0 0 0 0 1 0


Result             0 0 0 0 0 1 0

Notice that the result is the same as the value for Monday. So when our if statement is
evaluated, it returns true. Sure enough, we added Monday to the enumeration. You are
probably wondering how that happened, so let me explain it.

Remember that we are doing a logical AND operation. All of the conditions must be true in
order to return a true. By AND-ing the value for Monday and daysOfTheWeek, we
essentially are seeing if the position used for signaling Monday is set to true or not.
If daysOfTheWeek contains Monday (and in this case it does), the AND operation will
return the value for just Monday. We then do a conditional comparison to the value of
Monday. The logic can be applied to any day of the week you wish to check for.
If daysOfTheWeek does not contain the day you are looking for, the result will be 0 and
your conditional check will be false.

So what does the Flags attribute do?

So now that we understand the how and the why, people assume that the Flags attribute
sets up the values accordingly for you. The actual job of the attribute is for readability
purposes when debugging or writing the values to a console or a file.

                                                                                  Collapse | Copy Code

// Writes the following to the console window: Monday, Wednesday, Friday
Console.WriteLine(daysOfTheWeek);




A Neat Trick

Depending on the size of your enumeration, the exponential increase in values can get
tricky to keep track of or calculate in your head. There is a technique in C# that will make
this far more manageable, and it is an old one at that.

                                                                                  Collapse | Copy Code

[Flags]
enum DaysOfTheWeek
{
    Sunday = 1,
    Monday = 1 << 1,
    Tuesday = 1 << 2,
    Wednesday = 1 << 3,
    Thursday = 1 << 4,
    Friday = 1 << 5,
    Saturday = 1 << 6,
}


Through the use of bit shifting, we can move the 1 bit sequentially without having to worry
about the actual integer value. From a readability stand point, I prefer this. If you get into
the habit of thinking of flags as a series of bit switches, this allows to see which bit switch
you turned on for a specific value. There is nothing that requires you to do this, but it is a
neat trick that I like using which I thought would be worth sharing in this article.

For more information on this trick, check out the << Operator (C# Reference) article in the
MSDN library.

Contenu connexe

Dernier

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 

Dernier (20)

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 

En vedette

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

En vedette (20)

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 

Enum and flags in c#

  • 1. Ending the Great Debate on Enum Flags By virusstorm | 4 Jun 2012 | Article C#2.0C#3.0.NETDevBeginnerC#4.0 This article explains how enumeration and flags work in C#.NET and how to properly use them. See Also More like this More by this author Article Browse Code Stats Revisions (4) Alternatives 23 Download demo project - 10.8 KB Introduction Everyone learns about enumeration when they start developing in C#.NET. It is a very simply structure that can be used to solve a wide range of problems. One of the best uses for enumeration is using them as a flag. In the .NET Framework, there is a handy attribute you specify on your enumeration called Flags and now your enumeration will work as a flag. Or so you thought. The reality is there is work you need to do to make the enumeration to function like a flag. I have seen experienced .NET developers get caught up working under the assumption that Flags handles everything for you. I personally think the reason for this is due to the documentation surrounding enumeration and flags. It seems to me the MSDN documentation on enumeration fails to explain how enumeration and flags work which causes a misunderstanding at implementation time. In this article, I will explain what an enumeration is, what are flags, and how to make enumeration operate like a flag. Background A few months ago (from when this article was written), I was trying to recall a technique (which I will go over in this article) for setting up enumeration as flags. So I fired up my trusty web browser and brought up Google and searched for "enum flags". You get a little over two million results back and naturally, the first few are MSDN documentation pages. As I was hunting for the article that had the technique I wanted, I became increasingly frustrated with the mixed content and descriptions as to how enumeration and flags work. I got so frustrated between the authors and the comments people were leaving, I decided to set out and write an 'end all be all' solution to this problem. Also, this is my first article, so be kind when you leave comments and feedback.
  • 2. What is enumeration? Enumeration is basically a list of possible values for a variable. The easiest way to apply them is to think of the days of the week. Typically, Sunday is considered to be the first day of the week and Saturday as the last day of the week. So if we assign a numerical value to the days of the week, you get the following: Sunday = 1, Monday = 2, Tuesday = 3, Wednesday = 4, Thursday = 5, Friday = 6, and Saturday = 7. So in our program, we can use these numerical values for the day of the week as opposed to the string value. This removes issues related with string comparison (case issues, which space, special characters, etc.), and it also allows us to save memory and processing time. I know in today's world, memory usage and CPU usage are not a huge issue versus twenty years ago, but that doesn't give us the excuse to be lazy or design poorly. In the C#.NET world, we use the keyword enum to define an enumeration. If we use the day of the week example from above, we would see something like this: Collapse | Copy Code enum DayOfTheWeek { Sunday = 1, Monday = 2, Tuesday = 3, Wednesday = 4, Thursday = 5, Friday = 6, Saturday = 7, } The MSDN reference describes more ways and styles of defining an enumeration. I would suggest checking out theenum (C# Reference) for more details if you are new to enumeration. What are flags? The idea of flags is to take an enumeration value and allow it hold multiple values. If we continue with our days of the week example, think of a scheduling application. You want to set up a meeting for Monday, Wednesday, and Friday. The way we previously defined our enumeration, we would need seven DayOfTheWeek variables defined or create a list of DayOfTheWeek in order to store the days a meeting could be scheduled. If we change the way we defined our enumeration to support the use of flags, we could avoid adding seven variables or adding the overhead of a list. This is where life gets confusing. A lot of people believe that simply adding theFlags attribute to the enumeration will take care of this. That is not the case. To properly implement an enumeration as a flag, you need to have the values increasing by a power of two. So if we define our enumeration to do this, we would get the following: Collapse | Copy Code [Flags] enum DaysOfTheWeek { Sunday = 1, Monday = 2,
  • 3. Tuesday = 4, Wednesday = 8, Thursday = 16, Friday = 32, Saturday = 64 } The Why and the How So it is at this point, a lot of people get confused. Let us start off by understanding at a binary level, what is going on. Day of the Week Decimal Value Binary Value Sunday 1 0000001 Monday 2 0000010 Tuesday 4 0000100 Wednesday 8 0001000 Thursday 16 0010000 Friday 32 0100000 Saturday 64 1000000 What you see here, at a binary level, is a left shift of bits. This is a critical component to flags and how we work with them. Now let us take a look at how we assign values to them. Consider the following code snippet: Collapse | Copy Code DaysOfTheWeek daysOfTheWeek; daysOfTheWeek = DaysOfTheWeek.Monday | DaysOfTheWeek.Wednesday | DaysOfTheWeek.Friday; What we have just done here is assigned Monday, Wednesday, and Friday to the variable daysOfTheWeek. We did this by using an OR operator. Let us look at the binary level again and see what happened. Monday 0 0 0 0 0 1 0 | | | | | | | Wednesday 0 0 0 1 0 0 0 | | | | | | | Friday 0 1 0 0 0 0 0 Result 0 1 0 1 0 1 0
  • 4. Take note of the result. Remember that the single pipe, "|", in C# is a logic operator, not conditional. This means at a binary level, we OR each position of the binary values. Boolean logic tells us in an OR situation the result is true when at least one condition is true; otherwise the result is false. What you should begin to see now is that we are using each position of the binary value as a switch or flag to indicate when a day of the week has been selected. To test to see if our result value has a specific flag set, we would use the following syntax: Collapse | Copy Code if((daysOfTheWeek & DaysOfTheWeek.Monday) == DaysOfTheWeek.Monday) { // Contains Monday! } else { // DOES NOT Contain Monday! } Before we did a logical OR to combine the flags to store them into a single variable. Now we use the logical AND operator, "&", to test for the value. Let us look at the binary level to see what happened. daysOfTheWeek 0 1 0 1 0 1 0 & & & & & & & Monday 0 0 0 0 0 1 0 Result 0 0 0 0 0 1 0 Notice that the result is the same as the value for Monday. So when our if statement is evaluated, it returns true. Sure enough, we added Monday to the enumeration. You are probably wondering how that happened, so let me explain it. Remember that we are doing a logical AND operation. All of the conditions must be true in order to return a true. By AND-ing the value for Monday and daysOfTheWeek, we essentially are seeing if the position used for signaling Monday is set to true or not. If daysOfTheWeek contains Monday (and in this case it does), the AND operation will return the value for just Monday. We then do a conditional comparison to the value of Monday. The logic can be applied to any day of the week you wish to check for. If daysOfTheWeek does not contain the day you are looking for, the result will be 0 and your conditional check will be false. So what does the Flags attribute do? So now that we understand the how and the why, people assume that the Flags attribute sets up the values accordingly for you. The actual job of the attribute is for readability purposes when debugging or writing the values to a console or a file. Collapse | Copy Code // Writes the following to the console window: Monday, Wednesday, Friday
  • 5. Console.WriteLine(daysOfTheWeek); A Neat Trick Depending on the size of your enumeration, the exponential increase in values can get tricky to keep track of or calculate in your head. There is a technique in C# that will make this far more manageable, and it is an old one at that. Collapse | Copy Code [Flags] enum DaysOfTheWeek { Sunday = 1, Monday = 1 << 1, Tuesday = 1 << 2, Wednesday = 1 << 3, Thursday = 1 << 4, Friday = 1 << 5, Saturday = 1 << 6, } Through the use of bit shifting, we can move the 1 bit sequentially without having to worry about the actual integer value. From a readability stand point, I prefer this. If you get into the habit of thinking of flags as a series of bit switches, this allows to see which bit switch you turned on for a specific value. There is nothing that requires you to do this, but it is a neat trick that I like using which I thought would be worth sharing in this article. For more information on this trick, check out the << Operator (C# Reference) article in the MSDN library.