C # Conversion between value types and reference types

Source: Internet
Author: User

Conversion between value types and reference types is also known as a boxed unboxing operation.


Converting a base type or value type to a reference type is called boxing,


Changes a value type to a reference type. The meaning of the specific code is as follows:

1 int i=10: Defines an integer variable i and assigns an initial value.

2 Object obj=i: A reference type obj is defined and the value of I is assigned to obj to implement the boxing operation. Widening conversions, which can be implicitly converted.

3) I=20: Change the value of I, the value of obj does not change.

The essence of the implementation is that when the reference type obj is assigned, the integer variable i value 10 is copied into one area of memory (in the heap), and the pointer (in the stack) that holds the memory space in obj. So I change does not affect the value of obj.


Converting a reference type to a base type or a value type is called a unboxing.


A reference type is changed to a value type. The meaning of the specific code is as follows:

1) Object obj=10: Assigns a value to the reference type obj.

2 int i= (int) obj: Converts a reference type obj display to an integral type, implementing a unboxing operation. Here is the narrowing conversion, which must be explicitly converted.

3) i=20: Integer variable value changed, does not affect the value of obj.

A boxed unboxing operation is essentially an operation that increases the pointer and removes the pointer, which is a waste of resources and may cause errors. A thrown sysytem.invalidcastexception exception is thrown if the value of the referenced type of the unboxing is null or if it conflicts with the corresponding base type or value type.

Unboxing exception.

Object obj=10;

float f= (float) obj;

Description: The type designation does not match the value when unpacking. Because the obj value is an integral type, an exception will occur if you want to split the box into a floating-point type.

The reason that value types and reference types can be converted to each other is that their roots are object classes and implicitly inherit from the object class. Without this premise, they cannot be converted to each other.

In the final analysis, these conversions are the transformation of objects between relationships, and there is no inheritance, and transformations are not possible.

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.