if (<object> == )

后端 未结 3 476
故里飘歌
故里飘歌 2021-01-19 23:12

I can\'t understand, why the if statement in the picture below returns false. I hope you can explain it to me.

You can see, that the values and the typs

相关标签:
3条回答
  • 2021-01-19 23:40

    When you cast an int to an object, you are creating a new object. This is referred to as boxing.

    Any comparison will then be an object comparison. With objects, the == operator will check if the references are the same. It will not check if the two variable reference equivalent objects, it will check if the two variables reference one object.

    If you want to perform an int comparison, you must convert it back to an integer (unbox it).

    Alternatively, you can use Equals instead of the == operator.

    Here's a piece of code which demonstrates this:

    using System;
    
    public class Program
    {
        public static void Main()
        {
            int n = 3;
            object o1 = n;
            object o2 = n;
            Console.WriteLine("o1 == o2 is {0}", o1 == o2);
            Console.WriteLine("o1.Equals(o2) is {0}", o1.Equals(o2));
            Console.WriteLine("(int)o1 == (int)o2 is {0}", (int)o1 == (int)o2);
    
        }
    }
    

    The output:

    o1 == o2 is False

    o1.Equals(o2) is True

    (int)o1 == (int)o2 is True

    0 讨论(0)
  • 2021-01-19 23:48

    The problem is that the two object wrappers of int that you are comparing are different objects.

    Since object does not override == with a call of Equals, the comparison checks references instead, and returns false, because the two objects are different.

    You can use object.Equals(item, value) to avoid this problem.

    0 讨论(0)
  • 2021-01-19 23:51

    The == operator you are calling is the overload that takes two object parameters. This uses reference equality - the value isn't important, it has to be the same object.

    As you can read in the documentation:

    For reference types other than string, == returns true if its two operands refer to the same object. For the string type, == compares the values of the strings.

    While int is a value type, it has been 'boxed' (wrapped in an object). You are comparing the two different reference types that wrap your integers.

    To fix this, you can use object.Equals instead - this will compare the two integers.

    item.Equals(value);
    

    Or the static method (which would handle the case where item == null):

    object.Equals(item, value);
    

    If you unbox to int then you can use the int overload of == as you expect:

    (int)item == (int)value;
    

    Again, per the docs:

    For predefined value types, the equality operator (==) returns true if the values of its operands are equal.

    0 讨论(0)
提交回复
热议问题