Chapter 5 book notes

Source: Internet
Author: User

It is important to understand the. NET type.

The main types of. NET are value type and reference type.

Differentiate these two types

1. Memory

Value Type:

Including enum enumeration and structure struct

Memory Allocation:

The instance is usually allocated to the thread stack and does not include any pointer to the instance data. Because the variable itself contains the instance data, the memory cannot be controlled by GC. When the scope ends, the value type is released independently.

Reference Type:

Including class, interface, array, delegate

Memory Allocation:

The instance is allocated to the managed stack and stores the memory reference of the instance data. GC (Garbage Collection) controls its Collection.

Special cases:

When nesting occurs, that is, the value type is nested reference type, and the reference type is nested value type, how is the memory allocated?

A value-type instance is always allocated to its declared location, and a reference-type instance is always allocated to the managed stack.

2. the sizeof () operator is used to obtain the size of the value type, but not applicable to the reference type.

3. The value type uses the new operator or the initial value to complete initialization, but the simple definition does not complete initialization. In this case, the reference to the member cannot be compiled

4. The performance of the reference type is less than that of the value type.

5. The value type is inherited from System. ValueType, and System. ValueType is inherited from System. Object.

6. The value type is closed and cannot be used as the base class of other types. However, it can be a single inheritance or multiple inheritance interfaces. The reference type generally has inheritance.

7. The value type is not polymorphism, but the reference type is polymorphism.

Mutual conversion between value types and reference types

Packing and unpacking

Pass by value and pass by reference:

Pass by value

Whether it is passing by reference type by value or passing by value type, it copies an object instance on the stack and transmits the new instance without changing the value of the original Instance.

Pass by reference

Directly transmits the reference address pointing to the instance. if the address is changed, the parameter value is changed.

The parameter address is passed.

When the value type is passed by reference, direct operation is performed on the Value Type parameter instance.

Both Ref and Out are passed by reference. The difference is that the parameter must have been initialized before the Ref parameter is passed, and the out parameter is not required for passing.

Implementation of custom type conversion:

Static access modifier operator conversion modifier operator type (parameter list)

All conversions must be static. See P160.

If a class object is declared pointing to a managed heap address, but the class object changes the reference, the previous address will be recycled by GC.

Packing and unpacking


Convert value type data to non-type reference object


1. Memory Allocation

2. Copy an instance

3. Return address (this step is not included, but often occurs together)


Obtain the address of some fields from the value type in the boxed object,The execution does not include the copy process of fields.


1. instance check

2. pointer return

3. Copy Fields


1. After unpacking, you must ensure that the execution result is of the type when it is not packed.

2. The reference type remains in the packing status. packing and unpacking are for the value type.

We should try to avoid box packing and unpacking. You can consider using generic

ArrayList, HashTable, and Emun are easy to cause packing.

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: 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.