Checking if enum type value contains certain value

  • Thread starter Thread starter Pavils Jurjans
  • Start date Start date
P

Pavils Jurjans

Hello,

Is there some short-gand way to check if the enum type value contains
certain value?

Ie, the talk here is about Regex object:

Regex re = new Regex(@"pattern", RegexOptions.IgnoreCase |
RegexOptions.Multiline);
if ((re.Options & RegexOptions.IgnoreCase) == RegexOptions.IgnoreCase) ...
do stuff ...

This "if" expression is rather lengthy, perhaps there is some shorter way to
check wether the value contains one of the ORed enum values?

Thanks,

Pavils
 
Pavils Jurjans said:
Is there some short-gand way to check if the enum type value contains
certain value?

Ie, the talk here is about Regex object:

Regex re = new Regex(@"pattern", RegexOptions.IgnoreCase |
RegexOptions.Multiline);
if ((re.Options & RegexOptions.IgnoreCase) == RegexOptions.IgnoreCase) ...
do stuff ...

This "if" expression is rather lengthy, perhaps there is some shorter way to
check wether the value contains one of the ORed enum values?

Well, you don't need to check for equality - just "non-zeroness":

if ((re.Options & RegexOptions.IgnoreCase) != 0)

which is fairly straightforward.
 
Yes, that's true!

I was not aware that runtime will implicitly convert enum type to int so
that it can be compared to zero.

Thanks!

Pavils.
 
Back
Top