[Flags]
enum Colour
{
Black = 1, // 0001
Blue = 2, // 0010
Green = 4, // 0100
Yellow = 8 // 1000
}
static void Main(string[] args)
{
var isValid = Enum.IsDefined(typeof(Colour), 5); // 5 is 0101
}
Enum.IsDefined:
Returns an indication whether a constant with a specified value exists in a specified enumeration.
in your example you have values 1,2,4,8 why you thinking that value 5 should return true ??
[Flags]
enum Colour
{
Black = 1,
Blue = 785,
Green = 4,
Yellow = 666
}
static void Main(string[] args)
{
var isValid = Enum.IsDefined(typeof(Colour), 666); // true
var isValid2 = Enum.IsDefined(typeof(Colour), 785); // true
var isValid3 = Enum.IsDefined(typeof(Colour), 5); // false
}