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