What is the difference between = and equals in C #?

Source: Internet
Author: User
Tags true true

What is the difference between = and equals in C #?

What is the comparison process for value type and reference type?

Using system;
Using system. Collections. Generic;
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;
}
}

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 ();
}
}
}

Why is the answer true True False false true?

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 the values of the two variables, 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.
"Equals": whether the two variables represented by the Operation Reference 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 comparison between string a and string B is equal for the first output.
For the second pair of output object G = A and object H = B, there are two different objects in the memory, so 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.

Here is an introduction in msdn:
The following rules summarize the implementation principles of the equals method and the equal sign operator (=:

The gethashcode method is implemented every time the equals method is implemented. This ensures that equals and gethashcode are synchronized.

Every time the equality operator (=) is implemented, the equals method is rewritten to perform the same operation. In this way, the basic structure code (such as hashtable and arraylist) using the equals method is the same as the user code written using the equality operator.

The equals method must be rewritten each time icomparable is implemented.

When implementing icomparable, consider implementing equal (=) and not equal (! =), Less than (<), and greater than (>) operators.

Do not raise an exception in the equals, gethashcode method, or equal operator (=.

For more information about the equals method, see implement the equals method.

Implement the equal operator (=) in the Value Type)
In most programming languages, the default equality operator (=) is not used for value types. Therefore, the equal operator (=) should be overloaded as long as it is equal and meaningful ).

You should consider implementing the equals method in the value type because the default implementation and custom implementation of system...:. valuetype will not be executed.

The equals operator (=) is implemented each time you override the equals method ).

Implement the equal operator (=) in the reference type)
Most languages do provide the default equal operator (=) for the reference type. Therefore, be careful when implementing the Equal operator (=) in the reference type. Most reference types (even if the equals method is implemented) should not overwrite the equal operator (= ).

If the type is base types such as point, string, and bignumber, the equal operator (=) should be rewritten ). Every time you consider overloading the addition (+) and subtraction (-) operators, you should also consider overloading the equal operators (= ).

Well, the following is the question. I believe everyone knows the answer.

Console. writeline (2 + 2) = 4 );
Object S = 1;
Object T = 1;
Console. writeline (S = t );

String A = "hello ";
String B = string. Copy ();
String c = "hello ";

Console. writeline (A = B );
Console. writeline (object) A = (object) B );
Console. writeline (object) A = (object) C );

Answer: True, false, true, false, true

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.