I like to write enum or integer to pass option to my methods. Is there a pattern or a method in C# to check if an (int 1,2,4,8,...) option is true or false. I think it should easily be possible via binary functions.
class Program
{
public enum Option
{
Option_A = 1,
Option_B = 2,
Option_C = 4,
Option_D = 8,
}
static void Main(string[] args)
{
int activeOption = 5; // Means I activeted the Option_A and Option_C
if (IsOption(activeOption, Option.Option_A)) { /*do work*/ }
if (IsOption(activeOption, Option.Option_B)) { /*do work*/ }
if (IsOption(activeOption, Option.Option_C)) { /*do work*/ }
if (IsOption(activeOption, Option.Option_D)) { /*do work*/ }
}
private static bool IsOption(int activeOption, Option option)
{
/*Evaluate if IsOption is true or false*/
throw new NotImplementedException();
}
}
EDIT
Am I limited the number of options that I can create like this?