C # Boxing and unpacking principles

Source: Internet
Author: User

Transfer from http://jingyan.baidu.com/article/454316ab57a772f7a7c03aea.html

. NET contains a special object class that can accept values of any data type, and when the type passed or assigned is not a specific data type, the object class provides a common way to pass parameters and assign values. The value assigned to object must be a reference type and be stored in the managed heap.

Packing:

int age = 24;

Object refage= age;

As you can see, the first statement creates a variable age and places the value in the managed stack;

The second statement assigns the value of age to the reference type. It places the value 24 in the managed heap.

This value type is packaged as a reference type of procedure, called boxing.

Unpacking:

Conversely, the process of converting a reference type to a value type is called unpacking. Unpacking will cast the object to its original type. Unboxing the previous object.

int newage = (int) refage;

String newage = (string) refage;

The value of the unboxing must have the same type as the variable to which it is to be converted.

C # Boxing and unpacking principles

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.