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.