I want to parse a binary file.
I have 3 valid formats. in the binary file the format is represented by short
. but it can be only 0,1,2
I created enum to describe these formats.
When i wrote this code i saw this compiler error:
Operator '>' cannot be applied to operands of enum
and int
.
public enum FormatType
{
Type0 = 0,
Type1 = 1,
Type2 = 2
}
private FormatType _format;
public FormatType Format
{
get { return _format; }
set
{
// red line under value > 2.
if (value < 0 || value > 2) throw new FileParseException(ParseError.Format);
_format = value;
}
}
but there is no problem with value < 0
.
later i find out that i can compare enum with 0 but not with other numbers.
simply to fix this problem i can cast int to enum.
value > (FormatType)2
But no need to cast when comparing with 0 why?
value < 0