Given this enum
public enum UserStatus : byte
{
Approved = 1,
Locked = 2,
Expire = 3
}
why does this check always return false when usr.Status = 1
if(usr.Status.Equals(UserStatus.Approved))
return true;
return false;
The comparison seems to work – there is no compile time error or runtime exception. Please note I am not the author of this piece of code and would like to find out why the author chose enum of type byte and why this doesn’t work as it should.
Because you will have to cast.
The equals method will check if
UserStatusis anint(depending on the type you have defined at the propertyusr.Status). It will then return that is not (it is of typeUserStatus) thus returnfalseBetter code would be: