I faced a very stupid bug a few days ago. It was caused by this enum I get from third-party library:
[Flags]
public enum MyStatus
{
OKResponse = 0,
ResponseTooBig = 1,
ErrorMessage = 2,
NoResponse = 4,
...
}
I am used to check flags this way:
if ((status & MyStatus.OKResponse) != 0) {...}
but it doesn't work for MyStatus.OKResponse
, because it is zero. It is not a flag at all, it is an absence of all flags. Of course, when I found the bug, I realised OKResponse
was the only non error status, so it really means "no errors, no flags". However, I really don't find it obvious.
Is it a bad habit defining 0 as one of values in flags enum? What is the recommended way? What is the best way to check flags, that would work with "no flags" flag, too?