Byte enum comparison in C#

后端 未结 3 717
梦如初夏
梦如初夏 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 13:58

    That is because the Usr.Status contains an Integer and the UserStatus.Approved returns an String i.e., Approved. So, an integer of value 1 can not be equal to the String Approved. So, you must convert the Enum status also to an integer by the following code

    if (usr.Status == (int)(UserStatus.Approved))
                    return true;
           return false;
    

提交回复
热议问题