现在的位置: 首页 > 综合 > 正文

C#: boxing and unboxing

2012年10月14日 ⁄ 综合 ⁄ 共 524字 ⁄ 字号 评论关闭

We should know boxing and unboxing clearly. But what's the actual performance affect happened in that process? Below diagram can give a overview:

A boxing process will bring in two copies of the same data on both stack and managed heap finally; while the latter unboxing will bring in three copies of the same data: two on the stack and one on the managed heap. This can easily happen in following exampled case:

ArrayList prices = new ArrayList();
decimal amount1 = 7.50m;
decimal amount2 = 1.95m;
prices.Add(amount1);
prices.Add(amount2);
foreach (decimal amount in prices)
{
Console.WriteLine(“Amount: {0}”, amount);
}

抱歉!评论已关闭.