效能優化:裝箱與拆箱
阿新 • • 發佈:2020-12-14
裝箱:是在通過轉化或強制轉化將值型別視為引用型別時發生的,因此會引起堆記憶體分配。裝箱操作既可以顯示進行也可以隱式進行。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
* Author:W
* 裝箱與拆箱
*/
public class BoxingTest : MonoBehaviour {
void Start()
{
int i = 128;
//顯示地進行裝箱操作
object obj = i;
//拆箱操作
i = (int)obj;
//隱式裝箱
Init(i);
}
/// <summary>
/// 隱式裝箱操作
/// </summary>
/// <param name="v"></param>
private void Init(object v)
{
Debug.Log("Init 隱式地操作==="+v);
}
}