Checking if enum type value contains certain value

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
 
J

Jon Skeet [C# MVP]

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.
 
P

Pavils Jurjans

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top