=, Equal, ReferenceEqual in C ),

Source: Internet
Author: User

=, Equal, ReferenceEqual (reprinted) in C ),

1. ReferenceEquals, =, Equals
Equals, =, ReferenceEquals can be used to determine whether the two objects are equal.

A) ReferenceEquals
ReferenceEquals is a static Object method. It is used to compare whether two referenced objects are referenced by the same Object. It always returns false for the value type.. (Because the objects after the Box are always different, hehe)

B) = is a binary operator that can be overloaded. It can be used to compare whether two objects are equal.
For the built-in value type, = determines whether the values of the two objects are equal. It automatically performs necessary type conversion as needed, and returns true or false based on whether the values of the two objects are equal. For example:

Int a = 100;
Double B = 100;

If (a = B)
Console. WriteLine ("equal supports compare between different types !");

The above program will output:
Equal supports compare between different types!

For user-defined value types, if the = operator is not overloaded, = is not usable. For example:
Struct Userstruct1;
Userstruct1;
Userstruct1 B;

If (a = B)
Console. WriteLine ("can = reach this far ?")

The above Code cannot be compiled. You can use the overload to enable = to act on user-defined value types.

For the reference type, = the default behavior is the same as that of ReferenceEquals.Returns true only when two objects point to the same Reference. However, many classes in the. NET Framework overload =. For example, if the = of the String class is the same as the Equals class, determine whether the content of the two strings is equal. Therefore, we recommend that you do not use the = operator for the reference type defined by the system in the application to avoid different running results in the program.

C) Equals is used as the built-in Object method. Equals supports comparison between any two CTS objects.
Equals has a static method and a version that can be reloaded. The following program snippets explain the usage of the two methods,

Int a = 5;
Int B = 5;

If (Object. Equals (a, B ))
// You can also use if (a. Equals (B ))
{
Console. WriteLine ("a is equal to B ");
}

In fact, the results of these two versions are exactly the same. If the user reloads Equals, the users call the Equals after the user reloads them. The advantage of Equals's static method is that you do not have to consider whether the object to be compared is null.

The Equals method has different definitions for the value type and reference type,If the value type is the same and the value is the same (each struct member must be the same), Equals returns true; otherwise, false.ForReference type. The default behavior is the same as that of ReferenceEquals. true is returned only when two objects point to the same Reference.. You can Overload Equals as needed. For example, Equals of the String class is used to determine whether the content of two strings is equal.

StringBuilder a = new StringBuilder ();
A. Append ("the test ");
String s1 = a. ToString ();
String s2 = "the test ";

If (s2 = s1)
Console. WriteLine ("= returns true ");

If (Object. Equals (s2, s1 ))
{
Console. WriteLine ("equals returns true ");
}

If (Object. ReferenceEquals (s2, s1 ))
{
Console. WriteLine ("ReferenceEquals returns true ");
}

This instance will output:
= Returns true
Equals returns true

NOTE: For the String class, if s1 = "the test a" is directly declared, the output result will contain "ReferenceEquals returns true ",
By default, only one Copy is retained for the declared same String on the stack, so s1 and s2 will point to the same Reference

In C #, there are multiple comparison methods, including referenceequal, equals, and objective sto, but there are nuances between them.

Referenceequal instance

Class MyClass {

Static void Main (){
Object o = null;
Object p = null;
Object q = new Object ();

Console. WriteLine (Object. ReferenceEquals (o, p ));
P = q;
Console. WriteLine (Object. ReferenceEquals (p, q ));
Console. WriteLine (Object. ReferenceEquals (o, p ));
}
If the object points to a null reference, it returns true. In addition, p = q; this value assignment statement only copies the address to p for the value type, there is no deep copy. Therefore, true is returned when comparing references. If we compare o. equals (p), the compiler reports an error because the equals method cannot be null. Because of this, the referenceequals method is not welcomed by developers. In addition, equals is the virtual method of the instance, and referenceequals is the static method. = Of the primitive type, that is, Operator overloading, equals method determination, etc. Of course, there is also a static equals method. The only difference between the static equals method and the instance equals method is that when the instance equals compares two, if one is null or both are null, an exception is thrown, but the static equals method does not. The static equals method first checks if the two are null. If there is null, false is returned. If no null is found, then the instance equals method is called for comparison.

Memberwiseclone () is translated as intelligent member replication. Intelligence treats value types and reference types differently.

The MemberwiseClone method creates a superficial copy by creating a new object and then copying the non-static fields of the current object to the new object. If the field is of the value type, perform a step-by-step copy on the field. If the field is of reference type, the referenced object is copied but not referenced. Therefore, the original object and its counterparts reference the same object.

For example, an object named X references objects A and B. Object B References Object C. The child copy of X creates A new object X2, which also references objects A and B. In contrast, the deep copy of X creates A new object X2, which references the new objects A2 and B2, which are copies of A and B, respectively. B2 references the new object C2, which is a copy of C.

For deep copy and shallow copy, deep copy is to create an object with the same touch. The shallow copy is still the same object, but there are two counters, the two strong references point to it.

Blog original address: http://www.cnblogs.com/zagelover/articles/2741409.html
& In C Language

& Can be used as the bitwise AND or address fetch Operator
The following describes two usage methods:
1. bitwise and operation bitwise AND operator "&" are binary operators. Its function is the binary phase corresponding to the two numbers involved in the operation. The result bit is 1 only when the two binary numbers are 1. Otherwise, the result bit is 0. The number of involved operations is supplemented.
For example, 9 & 5 can be written as follows: 00001001 (Binary complement of 9) & 00000101 (Binary complement of 5) 00000001 (Binary complement of 1) Visible 9 & 5 = 1.
Bitwise AND operations are usually used to clear some bits or retain some bits. For example, if a clears the high eight bits of 0 and retains the low eight bits, it can be used as a & 255 operation (255 of the binary number is 0000000011111111 ).
2. Get the address
& As The unary operator, the result is the address of the right operation object.
For example, & x returns the address of x.
The address itself is an abstract concept used to indicate the logical location of an object in the memory.

& In C Language

& Can be used as the bitwise AND or address fetch Operator
The following describes two usage methods:
1. bitwise and operation bitwise AND operator "&" are binary operators. Its function is the binary phase corresponding to the two numbers involved in the operation. The result bit is 1 only when the two binary numbers are 1. Otherwise, the result bit is 0. The number of involved operations is supplemented.
For example, 9 & 5 can be written as follows: 00001001 (Binary complement of 9) & 00000101 (Binary complement of 5) 00000001 (Binary complement of 1) Visible 9 & 5 = 1.
Bitwise AND operations are usually used to clear some bits or retain some bits. For example, if a clears the high eight bits of 0 and retains the low eight bits, it can be used as a & 255 operation (255 of the binary number is 0000000011111111 ).
2. Get the address
& As The unary operator, the result is the address of the right operation object.
For example, & x returns the address of x.
The address itself is an abstract concept used to indicate the logical location of an object in the memory.

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.