"Boxing" (boxing) and "unboxing" in C # (unboxing)

Source: Internet
Author: User
Tags object object reference
Boxing and unboxing: Any value type, reference type can be converted from the object type. A boxing conversion is the implicit or explicit conversion of a value type to an object type, or the conversion of this value type to an interface type applied by the value type (interface-type). Boxing the value of a value type is to create an instance of object and copy the value to this object, and the data in the boxed object object is in the heap, and the address in the heap is on the stack. The value of the boxed type is assigned to the object as a copy. such as: int i = 10;object obj = i; Implicitly boxed Object obj = object (i); An explicit boxing if (obj is int)//int Console.WriteLine ("OK");

Console.WriteLine (obj. GetType ()); System.Int32
There are two ways to view the type of raw data that is wrapped in a referenced object that is wrapped later. To determine whether the original type is a given atomic type, with IS; If you want to return a string, you can use the GetType method of the object class.

A unboxing conversion is the explicit conversion of an object type to a value type, or an explicit conversion of an interface type into a value type that executes the interface. Note that the boxing operation can be implicit but the unboxing operation must be explicit. The unboxing process is divided into two steps: First, examine the object instance to see if it is a boxed value for the given value type. Then, copy the value of this instance to a variable of the value type. For example: int i = 10;object obj = i;int j = (int) obj; there are two ways to view the type of raw data wrapped in a wrapped reference object. To determine whether the original type is a given atomic type, with IS; If you want to return a string, you can use the GetType method of the object class.

A unboxing conversion is the explicit conversion of an object type to a value type, or an explicit conversion of an interface type into a value type that executes the interface. Note that the boxing operation can be implicit but the unboxing operation must be explicit. The unboxing process is divided into two steps: First, examine the object instance to see if it is a boxed value for the given value type. Then, copy the value of this instance to a variable of the value type. For example: int i = 10;object obj = i;int j = (int) 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.