Summary of operators = and equals ()

Source: Internet
Author: User

C # has two different types of equality: the reference equality and the value are equal. The two objects have the same value. For example, two integers with a value of 2 have equal values. Referencing equal means that the two objects to be compared are not two objects, but two "Object references". These two "Object references the same object.
1. For value types
For the value type, if the object value is equal, the equal operator (=) returns true; otherwise, false.
2. For reference types
For reference types other than string, if two objects reference the same object, the = returns true. For the string type, = compares the string value.
= The operation compares whether the values of the two variables are equal.
The equals () method compares whether the content of the two objects is consistent. Equals is used to compare whether the reference type is a reference to the same object.
First, let's take a lookProgram:

Code

1 Using System;
2 Using System. Collections. Generic;
3 Using System. LINQ;
4 Using System. text;
5
6 Namespace Csharpdemo
7 {
8 Class Person
9 {
10 Private String Name;
11
12 Public String Name
13 {
14 Get { Return Name ;}
15 Set {Name = Value ;}
16 }
17
18 Public Person ( String Name)
19 {
20 This . Name = Name;
21 }
22 }
23 Class Program
24 {
25 Static Void Main ( String [] ARGs)
26 {
27 Int I = 1 ;
28 Int J = 1 ;
29 Console. writeline (I = J ); // True
30 Console. writeline (I. Equals (j )); // True
31
32 String A = New String ( New Char [] { ' H ' , ' E ' , ' L ' , ' L ' , ' O ' });
33 String B = New String ( New Char [] { ' H ' , ' E ' , ' L ' , ' L ' , ' O ' });
34 Console. writeline ( = B ); // True
35 Console. writeline (A. Equals (B )); // True
36
37 Object G = A;
38 Object H = B;
39 Console. writeline (G = H ); // False
40 Console. writeline (G. Equals (h )); // True
41
42 Person p1 = New Person ( " Person " );
43 Person p2 = New Person ( " Person " );
44 Console. writeline (p1 = P2 ); // False
45 Console. writeline (p1.equals (P2 )); // False
46
47
48 Person p3 = New Person ( " Person " );
49 Person p4 = P3;
50 Console. writeline (p3 = P4 ); // True
51 Console. writeline (p3.equals (P4 )); // True
52
53 Console. readkey ();
54
55 }
56 }
57 }

the output result is marked after the corresponding row.
Why does this answer appear? Because the value type is stored in the memory stack (later referred to as the stack), the reference type variable is only the address of the reference type variable in the stack, and it is stored in the heap.
= The operation compares whether the values of the two variables are equal, and the referenced variables indicate whether the addresses of the two variables are the same in the heap, whether the content in the stack is the same.
The equals operation indicates whether the two variables reference the same object, that is, whether the content in the heap is the same.
string is a special reference type. in C # language, many methods (including equals () of string objects are overloaded ), make the string object use the same value type.
in the preceding example, the comparison between string a and string B is equal.
for two different objects in the memory of object g and object H, the content in the stack is different, so they are not equal. G. Equals (h) uses the string equals () method, so it is equal (polymorphism ). If you modify strings A and B as follows:
string a = "AA";
string B = "AA";
, the two comparisons of G and H are equal. This is because the system does not allocate memory to string B, but points "AA" to B. Therefore, a and B point to the same string (the string is optimized by memory in this case ).
for P1 and P2, they are also two different objects in the memory, so the addresses in the memory are definitely not the same, so p1 = P2 will return false, because P1 and P2 are references to different objects, p1.equals (P2) returns false.
for P3 and P4, P4 = P3, P3 assigns a reference to the object to P4. P3 and P4 are references to the same object, therefore, true is returned for both comparisons.

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.