SlideShare ist ein Scribd-Unternehmen logo
1 von 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.

Weitere ähnliche Inhalte

Kürzlich hochgeladen

Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsManeerUddin
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 

Kürzlich hochgeladen (20)

Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture hons
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 

Empfohlen

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
 

Empfohlen (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.