The difference between = = and equals in C #

Source: Internet
Author: User

usingSystem;Internal classperson{ PublicPerson (stringname) {Name=name; }     Public stringName {Get;Set; }}Internal classprogram{Private Static voidMain () {varA =New string(New[] {'h','e','L','L','o'}); varb =New string(New[] {'h','e','L','L','o'}); Console.WriteLine ("1.a = = B:"+ (A = = b));//TrueConsole.WriteLine ("2.a.equals (b):"+ a.equals (b));//True        Objectg =A; Objecth =b; Console.WriteLine ("3.G = = h:"+ (g = = h));//FalseConsole.WriteLine ("4.g.equals (h):"+ g.equals (h));//True        varP1 =NewPerson ("Jia"); varP2 =NewPerson ("Jia"); Console.WriteLine ("5.p1 = = P2:"+ (P1 = = p2));//FalseConsole.WriteLine ("6.P1. Equals (p2):"+ P1. Equals (p2));//False        varP3 =NewPerson ("Jia"); varP4 =P3; Console.WriteLine ("7.P3 = = P4:"+ (P3 = = p4));//TrueConsole.WriteLine ("8.p3. Equals (p4):"+ p3. Equals (p4));//TrueConsole.ReadLine (); }}

Note: In actual use, the. NET puts string into a value type. So don't look at the string as a reference type.

Because a value type is a stack that is stored in memory (hereafter referred to as a stack), a variable of the reference type is simply the address of the reference type variable stored in the stack, and itself stored in the heap.

The operation compares the values of the two variables for equality, and for the 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.

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.

So in the example above, the first pair of outputs, the two comparisons of string A and string B are equal.

The second pair of output object G = A and Object H = B, in memory two different objects, so the content in the stack is not the same, it is not equal. G.equals (h) uses the Equals () method of Sting to be equal (polymorphic). If the strings A and B are modified as follows: String a= "AA"; String b= "AA"; Then, the two comparisons of G and H are equal. This is because the system does not allocate memory for string B, only "AA" points to B. So a and B point to the same string (the string is optimized for memory in this assignment).

For P1 and P2, there are two different objects in memory, so the addresses in memory are definitely not the same, so P1==P2 returns false, and because P1 and P2 are references to different objects, P1.equals (P2) returns false.

For P3 and P4,P4=P3,P3, references to objects are assigned to P4,P3 and P4 are references to the same object, so both comparisons return true.

In the introduction of AH:

The following rules summarize the implementation criteria for the Equals method and the equals operator (= =):

The GetHashCode method is implemented every time the Equals method is implemented. This allows Equals and GetHashCode to be kept in sync. Each time the equality operator (= =) is implemented, the Equals method is overridden so that they perform the same operation. Thus, the behavior of the infrastructure code (such as Hashtable and ArrayList) using the Equals method is the same as the user code written with the equality operator. Override the Equals method every time you implement IComparable. When implementing IComparable, you should consider implementing equality (= =), unequal (! =), operator overloads less than (<) and greater than (>) operators. Do not throw an exception in Equals, the GetHashCode method, or the equality operator (= =). For information about the Equals method, see Implementing the Equals method. Implementing the equality operator (= =) in a value type does not have a default equality operator (= =) implementation for value types in most programming languages. Therefore, the equality operator (= =) should be overloaded as long as the equality is meaningful. You should consider implementing the Equals method in a value type, because System::. Neither the default implementation nor the custom implementation of ValueType is executed. The equality operator (= =) is implemented each time the Equals method is overridden. Implementing the equality operator (= =) in a reference type most languages do provide the default equality operator (= =) implementation for reference types. Therefore, you should be careful when implementing the equality operator (= =) in a reference type. Most reference types (even reference types that implement the Equals method) should not override the equality operator (= =). If the type is a base type such as point, String, BigNumber, the equality operator (= =) should be overridden. You should also consider overloading the equality operator (= =) whenever you consider overloading the addition (+) and subtraction (-) operators.

The difference between = = and equals in C #

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.