首頁  >  文章  >  後端開發  >  C# 中的裝箱與拆箱

C# 中的裝箱與拆箱

WBOY
WBOY轉載
2023-09-08 14:57:111118瀏覽

C# 中的装箱和拆箱

裝箱

裝箱是將值類型隱含轉換為參考類型。

拆箱

拆箱是將裝箱建立的參考類型明確轉換回值類型。

範例

讓我們看看範例程式碼片段 -

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

讓我們看另一個在 C# 中顯示陣列清單的範例 -

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

以上是C# 中的裝箱與拆箱的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除