Difference between = and Equals in C #

Source: Internet
Author: User

UsingSystem;
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;
}
}
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 ();
}
}
}
Output result: true, true, false, true, false, false, true, true.
Summary:
1. For value types, = is equivalent to equals, and both are content that stores information.
2. For the reference type, = compares the address of the reference type in the stack, and the equals method compares the content of the storage information of the reference type in the managed stack. Www.2cto.com
3. Special processing is required for the string class. It is a class that has internally processed the equals method and =, so = is equivalent to equals, and both are content that stores information.
4. For some custom classes, we need to overload the equals method. Otherwise, It defaults to the equals method of the base class (the base class uses the Equals method in the Object class if the base class does not overload the Equals method ), their comparison is also the address, rather than the content of the storage information of the reference type in the managed heap.
 
It's hard to understand.
The following output:
Person p1 = new Person ("jia ");
Person p2 = new Person ("jia ");
Console. WriteLine (p1 = p2); // output False
Console. WriteLine (p1.Equals (p2); // output False
5. For string, we need to emphasize the differences between constant strings and string variables. For example:
// Constant string
Stringx = "shocould it matter"; // point to the same address, that is, the so-called constant pool.
Stringy = "shocould it matter ";
Object c = x;
Object d = y;
Console. WriteLine (c = d); // Output True
Console. WriteLine (c. Equals (d); // Output True
// String variable
Stringa = new string (new char [] {'h', 'E', 'l', 'l', 'O'}); // different addresses are pointed, is dynamically allocated
Stringb = new string (new char [] {'h', 'E', 'l', 'l', 'O '});
Object g =;
Object h = B;
Console. WriteLine (g = h); // output False
Console. WriteLine (g. Equals (h); // Output True
They allocate different addresses in different ways, string x = "shocould it matter"; because its initial value is a constant, the address is allocated to the static storage area on the hosting stack, that is, the so-called constant pool, and the main string a = new string (new char [] {'h', 'E', 'l', 'l ', 'o'}); It is different. It is the dynamically allocated address on the hosting stack.

From dish-bird
 

 


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.