Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group enum members?

I want to be able to determine if an enum value belongs to a certain group. See the pseudo example:

[Flags]
public enum Animals
{
  Dog = 1,
  Cat = 2,
  WildAnimal = Dog | Cat,
  Fly = 4,
  Bee = 8,
  Insect = Fly | Bee
}

public static bool IsInsect(Animals animals)
{
  return Animals.Insect.Qualifies(animals);
}

public static bool Qualifies(this Animals groupName, Animals value)
{
  //Is there a bitwise operation for it?
}
like image 730
Shimmy Weitzhandler Avatar asked Jan 17 '23 22:01

Shimmy Weitzhandler


1 Answers

Use HasFlag method on enum.

http://msdn.microsoft.com/en-us/library/system.enum.hasflag.aspx

like image 52
Matej Avatar answered Jan 30 '23 00:01

Matej