For the value type, if the object value is equal, the equal operator (=) returns true; otherwise, false. 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.
For the comparison of value types, we will not describe them here. We will discuss the comparison of reference types below:
First, let's look at a program.
Copy codeThe Code is as follows: 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;
}
}
}
Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace ConsoleApplication1
{
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 =;
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 ();
}
}
}
What will the program output?
The answer is true, true, false, true, false, false, true, or true.
Why is this answer? 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 two variables are equal. For a referenced variable, it indicates whether the addresses of the two variables stored in the heap are the same, that is, whether the stack content is the same.
The equals operation indicates whether the two variables are referenced to the same object, that is, whether the content in the heap is the same.
String is a special reference type. in C #, many methods (including the equals () method) of the string object are overloaded ), make the string object use the same value type.
Therefore, in the preceding example, the two comparisons of string a and string B are 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 equals () method of sting, so it is equal (too many ). If you modify strings a and B 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 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 ).
P1 and p2 are two different objects in the memory, so the addresses in the memory must be different, 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, so true is returned for both comparisons.
If we rewrite the equals method of person:Copy codeThe Code is as follows: 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;
}
Public override bool Equals (object obj)
{
If (! (Obj is Person ))
Return false;
Person per = (Person) obj;
Return this. Name = per. Name;
}
}
}
P1.equals (p2) returns true.