Boxed and unboxing of C #

Source: Internet
Author: User

2018-08-12 23:03:05

Boxing is the process of converting a value type to a object type or to any interface type implemented by this value type. When the CLR is boxing a value type, it wraps the value inside the System.Object and stores the latter on the managed heap. Unboxing extracts the value type from the object. boxing is implicit; unboxing is explicit. The concept of boxing and unboxing is the basis for a unified view of the type System C #, where any type of value is treated as an object.

Icon:

Understanding: Boxing (boxing) assigns a value type to a reference type. From the stack (stack) to the heap (heap).

Unpacking (unboxing) returns the reference type to the value type variable. From heap to stack. Although not necessarily successful.

Example: Boxing

1 int 123 ; 2 // The following line boxes I. 3 object o = i;

Example: Unboxing

1 123 ; 2 i = (int) o;  // Unboxing

Boxed and unboxing of C #

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.