What is the difference between == and equals() in C#

高洛峰
Release: 2016-12-16 10:28:46
Original
1199 people have browsed it

 Such as the following code:

int age = 25;
 
short newAge = 25;
 
Console.WriteLine(age == newAge);  //true
 
Console.WriteLine(newAge.Equals(age)); //false
 
Console.ReadLine();
Copy after login

 int and short are primitive types, but comparison with "==" returns true, and equals() returns false. why?

 Answers:

In short:

 "equals()" is more complicated than "==".

 Specifically:

 The original type overrides object.Equals(object) of the base class, and returns true when the object in the brackets is the same as its type and value (note that the Nullable type is also suitable for the above judgment; non-empty Nullable Types are always boxed to a base type instance).

Since newAge is short, newAge.Equals(object) returns true when object is short and the value is equal to the newAge value. What you are passing is an int object, so it returns false.

In contrast, the "==" operator is defined as an operation with two integers (int) or two short integers (short) or two long integers (long). When the two parameters of "==" are an integer and a short integer, the compiler will implicitly convert short to int and compare the size of the converted int value.

 Other ways to make it work:

 Primitive types also have their own equals() method, equals accepts parameters of the same type.

  If you write age.Equals(newAge), the compiler will choose int.Equals(int) as the best overload method and implicitly convert short to int. Then, it will return true because this method directly compares the size of two int values.

  Short also has a short.Equals(short) method, but the int type cannot be implicitly converted to short, so it will not be called.

  You can force this method to be called using a cast conversion:

Console.Writeline(newAge.Equals((short)age)); //true
Copy after login

  This will call short.Equals(short) directly, without boxing. If age is greater than 32767, it will throw an overflow exception.

  You can also call the short.Equals(object) overload, but you need to explicitly pass a boxed object of the same type:

Console.WriteLine(newAge.Equals((object)(short)age)); // true
Copy after login

Like the previous optional method (short.Equals(short )), if the size exceeds the short range, an overflow exception will also be thrown. Unlike previous solutions, it boxes short into an object - a waste of time and memory.

  Source Code:

  Here is the Equals() used in practice:

public override bool Equals(Object obj) {
 
       if (!(obj is Int16)) {
 
           return false;
 
       }
 
       return m_value == ((Int16)obj).m_value;
 
   }
 
   public bool Equals(Int16 obj)
 
   {
 
       return m_value == obj;
 
   }
Copy after login

 



For more information about the difference between == and equals() in C#, please pay attention to PHP Chinese for related articles. net!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!