Byte enum comparison in C#

后端 未结 3 688
梦如初夏
梦如初夏 2021-01-11 12:56

Given this enum

public enum UserStatus : byte
{
    Approved = 1,
    Locked = 2,
    Expire = 3
}

why does this check always return false

3条回答
  •  执念已碎
    2021-01-11 14:02

    Because you will have to cast.

    The equals method will check if UserStatus is an int (depending on the type you have defined at the property usr.Status). It will then return that is not (it is of type UserStatus) thus return false

    Better code would be:

    return usr.Status == (int)UserStatus.Approved;
    

提交回复
热议问题