C # boxing and unboxing)

Source: Internet
Author: User
Tags unpack

I have learned C #ProgramMembers know that there are two data types in C #: Value and reference. But what are the differences between the two types? I believe it is hard to explain it to many people. Let's discuss this issue today.

In a word, the difference is thatDifferences in replication mechanisms.

The value type directly contains the value, that is, the variable reference location is the location where the value is actually stored in the memory. Therefore, if a value type is assigned to another value type, a memory copy of the original variable value is created at the position of the new variable. The second variable of the same value type cannot reference the same memory location as the first variable (assuming there is no out or ref parameter ). Therefore, changing the value of a variable does not affect the values of two other variables. The amount of memory required for the value type will be fixed during compilation and will not change during runtime. Because the size is fixed, the value type can be stored inStack(Stack) in the memory area.

The reference type stores the reference (memory address) of a memory location, where the real data can be found. Therefore, to access data, the "RunTime" must read the memory location from the variable and jump to the memory location that contains the data. The reference type pointing to the memory area is calledHeap(HEAP ).

In fact, friends who have learned assembly can easily think of the CPU addressing method.Direct addressingAndMemory indirect addressingIt is similar to the reference method of the above two value types. Although I did not verify, I believe there will be a certain relationship between them.

Because two different types exist, and we need to convert these two types in actual development, we need to know the working process. This is what we often callPackingAndUnpack.PackingTo put it simply, convert the value type to the reference type.UnpackIs to convert the reference type to the value type.

PackingProcess: 1. First, allocate the memory in the heap. It will be used to store value-type data and some necessary pointers. 2. copy the data on the stack to the allocated location on the stack. 3. The new object reference is updated, pointing to the location on the stack.

UnpackIn most cases, the data on the heap is copied immediately after the bin is split.

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.