Home > Backend Development > C#.Net Tutorial > What are the differences between equals and == in C#

What are the differences between equals and == in C#

高洛峰
Release: 2016-12-16 10:30:26
Original
1715 people have browsed it

There are two different kinds of equality in C#: reference equality and value equality.


◎Value equality is equality in the commonly understood sense: it means that two objects contain the same value.
◎Reference equality means that what is being compared is not two objects, but two object references, and both refer to the same object.

Equals:


In the following statement, x, y and z represent non-null object references.

* x.Equals(x) returns true except when floating point types are involved.
* x.Equals(y) returns the same value as y.Equals(x).
* x.Equals(y) returns true if both x and y are NaN.
* (x.Equals(y) && y.Equals(z)) returns true if and only if x.Equals(z) returns true.
* As long as the objects referenced by x and y are not modified, successive calls to x.Equals(y) will return the same value.
* x.Equals(null) returns false.

==:


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

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

The difference between Equals and ==

"==": The operation compares whether the values ​​​​of two variables are equal. For reference variables, it means whether the addresses stored in the heap of the two variables are the same, that is, in the stack are the same.

"equals": Whether the two variables represented by the operation are references to the same object, that is, whether the contents in the heap are the same.
String is a special reference type. In the C# language, many methods of the string object (including the equals() method) are overloaded, making the string object used like a value type.
Example

C# code

using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 
namespace ConsoleApplication1
 {
     class Person
     {
         private string name;
         public string Name
         {
             get { return name; }
             set { name = value; }
         }
         public Person(string name)
         {
             this.name = name;
         }
     }
     class program
     {
         static void Main(string[] args)
         {
             string a = new string(new char[] { 'h', 'e', 'l', 'l', 'o' });
             string b = new string(new char[] { 'h', 'e', 'l', 'l', 'o' });
             Console.WriteLine(a == b);
             Console.WriteLine(a.Equals(b));
             object g = a;
             object h = b;
             Console.WriteLine(g == h);
             Console.WriteLine(g.Equals(h));
             Person p1 = new Person("jia");
             Person p2 = new Person("jia");
             Console.WriteLine(p1 == p2);
             Console.WriteLine(p1.Equals(p2));
             Person p3 = new Person("jia");
             Person p4 = p3;
             Console.WriteLine(p3 == p4);
             Console.WriteLine(p3.Equals(p4));
             Console.ReadLine();
         }
     }
 }
Copy after login

Output result

true, true, false, true, false, false, true, true.

The summary is as follows:


1. For value types, == and equals are equivalent and both compare the contents of stored information.
2. For reference types, == compares the address of the reference type in the stack, while the equals method compares the content of the reference type’s stored information in the managed heap.
3. The string class needs special treatment. It is a class that has already processed the equals method and == internally. Therefore, == and equals are equivalent and both compare the contents of stored information.
4. For some customized classes, we need to overload the equals method, otherwise it defaults to the equals method of the base class (if the base class does not overload the Equals method, it will be the Equals method in the Object class), and their comparison is also The address, rather than the content of the reference type, stores information in the managed heap.


For more related articles on the differences between equals and == in C#, please pay attention to the PHP Chinese website!


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