using System;
using System.Collections;
using System.Collections.Generic;
/*
* 装箱和拆箱操作很容易使用,但性能损失比较大,遍历许多项时尤其如此。
*
*/
namespace Csharp
{
class Program
{
static void Main(string[] args)
{
var list = new ArrayList();
list.Add(44); //boxing--convert a value type to a reference type
int i1 = (int)list[0]; //unboxing--convert a reference type to a value type
var list1 = new List<int>();
list1.Add(55); //no boxing -- value types are stored in the List<int>
int i2 = list1[0]; //no unboxing, no cast needed
foreach (int item in list)
{
Console.WriteLine(item); //unboxing
}
foreach (int item in list1)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
}
C#的装箱和拆箱
最新推荐文章于 2024-06-19 10:56:11 发布