Compare the packing and unpacking types and the packing types

Source: Internet
Author: User

Compare the packing and unpacking types and the packing types

Recently, I am reading CLR via C #. I will review it and forget to take a note.

 

Packing and unpacking

1. Packing, value type conversion to reference type:

Allocate memory in the managed heap. The amount of memory allocated is the amount of memory required for each field of the type, the amount of memory required for type object pointers, and the amount of memory required for synchronizing block indexes.

Copy fields of the value type to the allocated memory.

Returns the object address. The current object address is an object reference.

2. Unpack and convert the reference type to the value type:

Obtains the unpacked part of the packed type, that is, each field of the object's original value.

Copy the field value from the heap to the value type instance in the stack

 

Therefore, you do not need to allocate memory for unpacking, but you must copy all of them.

 

= Comparison of Equals

 

Compare the reference types, ==compare the reference addresses of the two parameters. When equlas is called, if the type or base class (except the object) does not overwrite the object. the equlas method calls the object. the equlas method compares two types of reference addresses. Otherwise, the equlas method of this type or the base class override (such as string) is called ).

Compare the value types, = compare the values of the two parameters. When equlas is called, because the base class System. ValueType of the Value Type overwrites equlas, the values of the two parameters compared during the comparison are also compared.

 

Object a = new Test {a = 1 };
Object B = new Test {a = 1 };

Console. WriteLine (a = B); // false because the referenced address is compared, the two referenced object reference addresses are naturally different.
Console. WriteLine (a. Equals (B); // false because the Test class does not overwrite equlas, the Object. equals is called, and the equlas of the Object compares the referenced address.

 

Object c = 1;
Object d = 1;

Console. WriteLine (c = d); // false both parameters are boxed and become reference types. Therefore, the reference addresses are different.
Console. WriteLine (c. Equals (d); // true, the base class System. ValueType of the Value Type overrides equlas so that the values of the two parameters compared during comparison

 

Objects e = "123 ";
Objects f = "123 ";

Console. writeLine (e = f); // The true string type is optimized, so the memory is not allocated to f separately, but the "123" that has been allocated memory points to f, so the reference address is the same.
Console. writeLine (e. equals (f); // true System. string overrides equlas so that the values of the two parameters compared during the comparison (however, because the reference address is the same here, true is returned if there is any rewrite)

 

Object l = string. Copy (e. ToString ());
Console. WriteLine (l = e); // false copy directly. In addition, the allocated memory copy value is the same as the common reference object, so the reference address is different.
Console. writeLine (l. equals (e); // true System. string overrides equlas to compare the values of the two parameters. Therefore, true is returned if the reference address is different but the value is the same.

 

 

Int h = 1;
Int I = 1;


Console. WriteLine (h = I); // true value type = compare two values
Console. WriteLine (h. Equals (I); // true, the base class System. ValueType of the Value Type overrides equlas so that the values of the two parameters compared during comparison

 

Summary:

Finally, for the value type,= Compare with equlas. For reference types,= Compare referenced addressesIf the Object. equlas method, compare address, and overwrite are not overwritten, the method to be overwritten is called.

 

ReferenceEquals

Object static type method. Compare the reference addresses of two parameters. It is similar to the = Operator, but = can be overloaded. Therefore, it is best to compare the reference address of the reference type.

 

Equlas Rewriting

1. Determine whether the passed value is null. If it is null, false is returned.

2. Determine whether the passed value and this reference the same address. If the same address is used, true is returned.

3. Determine whether the type of the passed value is the same as that of this. If the type is different, the values cannot be equal. false is returned.

4. Compare the passed Value Field with this field. If there is any inconsistency, false is returned.

5. Call the equlas of the base class. If it is true, true is returned. If it is false, false is returned.

 

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.