. NET Boxing unpacking

Source: Internet
Author: User



When it comes to packing and unpacking, it is necessary to summarize briefly what is packing and unpacking,

Boxing: The value type is actually converted to a reference type.

Unpacking: In fact, the reference type is converted to a value type.


Value types: generally include int char bool double datetime and so on these, value types are stored inside the stack

Reference type: The reference type includes the object class Interface Delegate string Arry Dynamic, the reference type is stored in the heap, and the memory address is generally contiguous.



Boxing is the conversion of a value type to a reference type

int i = 12;
Object obj = i;


Unpacking is the conversion of a reference type to a value type
i = (int) obj;


Packing and unpacking are more consumption performance, can avoid such behavior as far as possible.


String STR3 = "123";
int i4 = 4;
Console.WriteLine (STR3+I4);//This boxing behavior will affect the efficiency, how to avoid, the following method
Console.WriteLine (Str3+i4. ToString ());//This is a way to improve point efficiency.
Console.readkey ();

. NET Boxing unpacking

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.