Ok, so I have been working with enums lately, and have come across a few different uses for them, and for the most part, I see how they can be useful.
The problem is that in some cases, the uses seem a little forced, and I am wondering if I am missing something. The main point I am speaking to is the use of bitwise operators to create what amount to arrays as far as I can tell:
The above allows me to use the bitwise '&' as follows:Code:enum tasks
{
task = 1,
objective = 2,
mission = 4,
milestone = 8,
all = task | objective | mission | milestone
}
The example I read was using it in a permissions scenario, using a method to check if the permissions fed into the method match a specific enum and returning a true or false.Code:Console.WriteLine(((int)tasks.objective & (int)tasks.all) == 2);
That is all and good, but I don't understand why it is better than creating an array or list of values and just running it against that:
I figure I might be limiting the potential use cases, but my immediate thought is generally that of validating user input. Of course, that would involve a little more code (i.e. converting to same case, etc.), but I think that is irrelevant because both solutions would be similarly affected.Code:List<string> myList = new List<string>() { "task", "objective", "mission","milestone" };
//OR
//string[] myArray = { "task", "objective", "mission", "milestone" };
Console.WriteLine(myList.Contains("mission"));
Am I missing something about the value of treating enums in that way?