- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What does the [Flags] Enum Attribute mean in C#?
The Enum Flags is used to take an enumeration variable and allow it hold multiple values. It should be used whenever the enum represents a collection of flags, rather than representing a single value
Use the FlagsAttribute for an enumeration only if a bitwise operation (AND, OR, EXCLUSIVE OR) is to be performed on a numeric value.
Define enumeration constants in powers of two, that is, 1, 2, 4, 8, and so on. This means the individual flags in combined enumeration constants do not overlap.
Example
class Program { [Flags] enum SocialMediaFlags { None = 0, Facebook = 1, Twitter = 2, LinkedIn = 4, Instagram = 8, Snapchat = 16, Pinterest = 32, Reddit = 64 } static void Main() { var SocialMedia1 = SocialMediaFlags.Facebook | SocialMediaFlags.Twitter | SocialMediaFlags.Instagram; var SocialMedia2 = SocialMediaFlags.LinkedIn; var SocialMedia3 = SocialMediaFlags.Pinterest | SocialMediaFlags.Reddit; SocialMediaFlags[] SocialMediasFlags = { SocialMedia1, SocialMedia2, SocialMedia3 }; for (int ctr = 0; ctr < SocialMediasFlags.Length; ctr++) if ((SocialMediasFlags[ctr] & SocialMediaFlags.Facebook) == SocialMediaFlags.Facebook) { Console.WriteLine("SocialMedia {0} has Facebook service: {1}", ctr + 1, "Yes"); } Console.WriteLine(); } }
Output
SocialMedia 1 has Facebook service: Yes
Advertisements