Home  >  Article  >  Backend Development  >  C# Learning Diary 18----Boxing conversion and unboxing conversion

C# Learning Diary 18----Boxing conversion and unboxing conversion

黄舟
黄舟Original
2017-01-21 15:08:551457browse

After the previous study, we can basically realize the conversion between value type and value type, and the conversion between reference type and reference type, but how to realize the conversion between value type and reference type (object)? ? ?

In C#, we use boxing to convert value types into reference types, and unboxing to convert reference types into value types; in other words, it can implement conversion from any value type, reference type To convert between Object types, with boxing and unboxing, any type can be regarded as an object type (for information about the object type, click Reference Type----Object Class to enter)

Boxing conversion:

I would like to add here that in .NET, data types are divided into value types and reference (not equivalent to C++ pointers) types. Correspondingly, memory allocation is divided into There are two ways, one is stack and the other is heap, (managed heap.) Value types will only be allocated on the stack. Reference types allocate memory with the managed heap.

Write an example:

  int i = 123;
           object o = i;    //实现装箱操作,其实也是隐式转换。

The schematic diagram of boxing conversion is as follows:

C# Learning Diary 18----Boxing conversion and unboxing conversion

The essence of boxing conversion The above is a copy type conversion, which means that when we complete the boxing, changing the value of i and the value of o will not change

Unboxing conversion:

                                                                                                                                                                                                                                                                          the The boxing value, then, copies the value of this instance to the variable of the value type;

Write an example (based on the above boxing code):

  int n_int = (int)o;    //强制将object类型转换为int 类型

C# Learning Diary 18----Boxing conversion and unboxing conversion

It can be seen that it is the reverse process of boxing, which forces the object to be converted to the original type. It must be noted that the unboxing value must have the same type as the target variable it is to be converted to.

What I need to emphasize here is that boxing and unboxing are conversions from value types or reference types to object types, and conversions from object types to value types or reference types.

The above is the content of C# Learning Diary 18----boxing conversion and unboxing conversion. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn