C # basic enhancement 1: difference between equals () and operator =

Source: Internet
Author: User

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 take a lookProgram

Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;

Namespace Leleapplication1
{
Class Person
{
Private   String Name;

Public   String Name
{
Get   {ReturnName ;}
Set   {Name=Value ;}
}

Public Person ( String Name)
{
This. Name=Name;
}
}
}

 

Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;

Namespace Leleapplication1
{
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 ( = 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 ();
}
}
}

what will be output when running the program?
the answer is true, true, false, true, false, false, true, or true.
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 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";
, 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.
if we rewrite the equals method of person:

Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;

Namespace Leleapplication1
{
Class Person
{
Private   String Name;

Public   String Name
{< br> 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.

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.