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

Source: Internet
Author: User
There are two different kinds of equality in C #: reference equality and value equality.


The equality of values is generally understood in the sense of equality: it means that two objects contain the same value.
Reference equality means that the comparison 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 in cases involving floating-point types.
* X.equals (Y) returns the same value as Y.equals (x).
* If both x and Y are NaN, x.equals (y) returns True.
* When and only if X.equals (z) returns True, (X.equals (y) && y.equals (z)) returns True.
* Successive calls to X.equals (Y) will return the same value as long as the objects referenced by x and Y are not modified.
* X.equals (NULL) returns FALSE.

==:


For predefined value types, the equality operator (= =) returns True if the value of the operand is equal, otherwise false is returned.

For reference types other than string, if the two operands refer to the same object, = = Returns True.
for string type, = = compares the value of a string.

The difference between equals and = =

"= =": The action compares whether the values of two variables are equal, for a reference variable represents whether the two variables are stored in the same address in the heap, that is, whether the contents of 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 of the heap are the same.
While a string is a special type of reference, in the C # language, many of the method methods of the string object, including the Equals () method, are overloaded, making the string object look like a value type.
Instance

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;}         The public person (string name) {this.name = name; }} class Program {static void Main (string[] args) {String a = new string (New CH             Ar[] {' 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 (); }     } }

Output results

True, True, False, True, False, False, true, true.

Summarized as follows:


1, for value types, = = and equals equivalent, are compared to store information content.
2. For reference types, = = compares the address of the reference type on the stack, and the Equals method compares the contents of the stored information of the reference type in the managed heap.
3, for the string class to special processing, it is an internal has handled the Equals method and = = class, so = = and equals equivalent, are compared to store information content.
4. For some custom classes, it is necessary to overload the Equals method, otherwise it defaults to the Equals method of the base class (the base class does not overload the Equals method to the Equals method in the object class), and their comparison is also the address, Instead of referencing the contents of a type's stored information in the managed heap.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.