Detailed explanation of C# boxing and unboxing principles

高洛峰
Release: 2017-01-24 14:27:30
Original
1638 people have browsed it

.NET contains a special Object class that can accept values ​​of any data type. When the type passed or assigned is not a specific data type, the object class provides a general method for passing parameters and assigning values. . The value assigned to object must be of reference type and stored in the managed heap.

Boxing:
int age = 24;
object refAge= age;
As you can see, the first statement creates a variable age and places the value on the managed stack ;
The second statement assigns the value of age to the reference type. It places the value 24 in the managed heap.
The process of packaging this value type into a reference type is called boxing.

Detailed explanation of C# boxing and unboxing principles

Unboxing:
Conversely, the process of converting a reference type to a value type is called unboxing. Unboxing will coerce the object to its original type. Unbox the previous object.
int newAge = (int) refAge;
string newAge =(String) refAge;
The unboxed value must have the same type as the variable it is being converted to.

Detailed explanation of C# boxing and unboxing principles

Through the simple illustration above, do you have a general understanding of the principles of C# boxing and unboxing?

More C# boxing and unboxing For detailed explanation of the principles and related articles, please pay attention to the PHP Chinese website!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!