
int myInt = 1; short myShort = 1; object objInt1 = myInt; object objInt2 = myInt; object objShort = myShort; Console.WriteLine(myInt == myShort); // scenario 1 Console.WriteLine(myShort == myInt); // scenario 2 Console.WriteLine(myInt.Equals(myShort)); // scenario 3 Console.WriteLine(myShort.Equals(myInt)); // scenario 4 Console.WriteLine(objInt1 == objInt1); // scenario 5 Console.WriteLine(objInt1 == objShort); // scenario 6 Console.WriteLine(objInt1 == objInt2); // scenario 7 Console.WriteLine(Equals(objInt1, objInt2)); // scenario 8 Console.WriteLine(Equals(objInt1, objShort)); // scenario 9 int myInt = 1; short myShort = 1; object objInt1 = myInt; object objInt2 = myInt; object objShort = myShort; Console.WriteLine(myInt == myShort); // scenario 1 true Console.WriteLine(myShort == myInt); // scenario 2 true Console.WriteLine(myInt.Equals(myShort)); // scenario 3 true Console.WriteLine(myShort.Equals(myInt)); // scenario 4 false! Console.WriteLine(objInt1 == objInt1); // scenario 5 true Console.WriteLine(objInt1 == objShort); // scenario 6 false!! Console.WriteLine(objInt1 == objInt2); // scenario 7 false!!! Console.WriteLine(Equals(objInt1, objInt2)); // scenario 8 true Console.WriteLine(Equals(objInt1, objShort)); // scenario 9 false!?! == operator means. In C #, there are more than a dozen built-in operators == for comparing different types. object == object string == string int == int uint == uint long == long ulong == ulong ... int == short or short == int operators, the most appropriate operator must be selected. In our case, this operator is int == int . Thus, the short converted to an int and then the two variables are compared by value. Therefore, they are equal.int , and it implements the three Equals methods. Equals(object, object) // object Equals(object) // object Equals(int) // IEquatable<int>.Equals(int) int as a parameter is more suitable, it is always better to convert a short argument to an int than an object . Therefore, Equals(int) will be called, which compares two variables of type int using comparison by value, so this expression is true.Equals method will be called. The caller has a short type, which again has three Equals methods. Equals(object, object) // object Equals(object) // object Equals(short) // IEquatable<short>.Equals(short) int to short . short.Equals(object) leaves the short.Equals(object) method, the implementation of which is equal to the following code: bool Equals(object z) { return z is short && (short)z == this; } true , the packaged element must be of the short type, and after unpacking it must be equal to the instance that caused Equals . But, since the argument passed is of type int , the method will return false .object == object will be chosen, which is equivalent to calling the Object.ReferenceEquals method. Obviously, the two links are equal in the fifth case and unequal in the sixth and seventh . The values contained in object type variables are unimportant, because comparison by value is not used at all, only references are compared.Object.Equals method will be used, which is implemented as follows: public static bool Equals(object x, object y) { if (ReferenceEquals(x, y)) return true; if (ReferenceEquals(x, null)) return false; if (ReferenceEquals(y, null)) return false; return x.Equals(y); } null , therefore, int.Equals(object) will be called, which, as you can assume when looking at the code of the short.Equals(object) method, is implemented as follows: bool Equals(object z) { return z is int && (int)z == this; } int , a comparison will be made by value and the method will return true . In the ninth case, the packaged variable is of type short , hence the type check ( z is int ) will fail, and the method will return false .true . If you think this is crazy and everything is confusing, you are right! Comparison in C # is very insidious.Source: https://habr.com/ru/post/209412/
All Articles