C # how to determine whether two objects are equal

Source: Internet
Author: User

In C #, the object is equal. This is to judge the reference type, rather than the value type. If the value is equal to the string or value, you only need to use the = Operator.

Use the = Operator to determine whether the addresses of the two objects in the memory are the same.

 

1. instantiate a class only once

Assign values to those references. If a new object is not instantiated, the assigned objects point to the same address. For example, there is a classPeople

People p1=new  People();People p2=p1;People p3;p3=p2;

Objects such as P1, P2, and P3 point completely to the same memory address. They are actually the same object. True is returned for all judgments using = or equal;

2. instantiate a class multiple times, but the initial values of each instance are the same

Declare two objects, each of which is instantiated once, and set the data of each class, that is, the field or attribute, to a unified initial value, as follows:

 

People p1=new People();p1.Name="haha";People p2 =new People();p2.Name="haha";

 

Assume that the people class has a property named name, so I instantiate the P1 and P2 objects, but the object obtained with P1 = P2 is false, this proves that the object can only be compared with the reference address of the object name using the = Operator. For example, the object is instantiated twice, in the memory space, different spaces are allocated, that is, different addresses, although the data in their content is the same.

Note: What should we do if we want to compare the data of an object in the memory to be equal, but not the memory address.

We all know that if the two memory addresses are equal, then the two objects are essentially the same object, but these two objects have different names in the program, to compare whether different objects are equal (not equal addresses ). You should rewrite the equal method of the object to determine whether the data of the two objects is equal. If the data of the two objects is equal, true is returned. If one item is not equal, false is returned. The specific instance is not demonstrated.

 

If something is wrong or bad, I hope you can correct it.

 

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.