Boxing and unboxing in C#

WBOY
Release: 2023-09-08 14:57:11
forward
1164 people have browsed it

C# 中的装箱和拆箱

Boxing

Boxing is the implicit conversion of a value type to a reference type.

Unboxing

Unboxing is the explicit conversion of the reference type created by boxing back to a value type.

Example

Let’s see a sample code snippet -

// int
int myVal = 12;
// Boxing
object myBoxed = myVal;
// Unboxing
int myUnBoxed = (int) myBoxed;
Copy after login

Let’s see another example of displaying an array list in C# -

int a = 5;
ArrayList arr = new ArrayList();
// Boxing
arr.Add(a);
// UnBoxing
int b = (int)arr[0];
Copy after login

The above is the detailed content of Boxing and unboxing in C#. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
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!