Binning and unboxing in. Net

Source: Internet
Author: User

Boxing and unboxing are new concepts proposed by. NET!
All types of. NET are inherited by the base class System. Object, including the most common basic types: int, byte, short, bool, etc., that is, all objects are objects. If you declare that these types of memory are allocated in HEAP, it will cause extremely low efficiency.

How does. NET solve this problem? By dividing a type into values and regerencetype ), the value types defined in C # include Sbyte, Byte, Short, Ushort, Int, Uint, Long, Ulong, Char, Float, Double, Bool, and Decimal), enumeration (enum), structure (struct), reference types include: Class, array, interface, Delegate, String, etc.
The value type is used to allocate memory in the stack and initialize it at the same time as the Declaration to ensure that the data is not NULL;
The reference type allocates memory in the heap and is initialized to null. The reference type requires garbage collection to recycle the memory. If the value type is not used, the system will automatically release the memory when it exceeds the scope of the function!
The following is the definition of packing and unpacking!
Packing is to implicitly convert a value type to a reference object. For example:
Int I = 0;
Repeated E. Object obj = I;
This process is packed! It is to pack I!
Unpacking is to convert a referenced object into any value type! For example:
Int I = 0;
System. Object obj = I;
Int j = (int) obj;
The first two sentences in this process are packing I, and the last one is unpacking obj!
Write another code to check the installation and unpacking times!
Int I = 0;
System. Object obj = I;
Console. WriteLine (I + "," + (int) obj );
Three packing and one unpacking occurred in total! ^ _ ^. Can you see it ?!
The first is to pack I, and the second is to convert I to the string type when the output is made. The string type is the reference type, that is, the packing is made, and the third packing is (int) convert obj to string type, boxed!
Split the box is (int) obj, and split the obj!

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.