Enum Boxing and Equality

后端 未结 2 613
眼角桃花
眼角桃花 2021-02-07 09:59

Why does this return False

    public enum Directions { Up, Down, Left, Right }

    static void Main(string[] args)
    {
        bool matches = IsOneOf(Directi         


        
2条回答
  •  离开以前
    2021-02-07 10:36

    As JB says, boxing. You can see this by changing from Enum to Directions:

    public static bool IsOneOf(Directions self, params Directions[] values)
    {
        foreach (var value in values)
            if (self == value)
                return true;
        return false;
    }
    

    true is returned.

提交回复
热议问题