?????????????????List??????????ù????????????п????List??????????????????????10??????List??????????????????????α仯???????MSDN?????List??Capacity??????????????????????????????????????С?????Capacity??????List????????μ???????????????????????????????????棬???????????????????????Capacity ??????List?????????????????????????????????棬?????????Ч??????????????????
?????????????List<T> list = new List<T>();????????List???????.Net Framework??????????????????????????list???????????????????list???ж???item????????????list???????item???list????????洢4??Item????洢??????Capacity??4????????????????????item????????Capacity????8??????list????????????????Capacity????????????????????·?????棬??????????????item???Capacity????12????16??????list?????????????????????item??????????????????????е?item??????????????????μ???檔
??????????????????list?????????????????list?????????????棬???????????????????????棬????????????????????????????
??????????????Capacity??????list???????????????????TrimExcess()????????δ??????檔
class Program
{
static void Main(string[] args)
{
List<Part> parts = new List<Part>();
Console.WriteLine(" Capacity: {0}"?? parts.Capacity);
parts.Add(new Part() { PartName = "crank arm"?? PartId = 1234 });
parts.Add(new Part() { PartName = "chain ring"?? PartId = 1334 });
parts.Add(new Part() { PartName = "seat"?? PartId = 1434 });
parts.Add(new Part() { PartName = "cassette"?? PartId = 1534 });
parts.Add(new Part() { PartName = "shift lever"?? PartId = 1634 });
Console.WriteLine();
foreach (Part aPart in parts)
{
Console.WriteLine(aPart);
}
Console.WriteLine(" Capacity: {0}"?? parts.Capacity);
Console.WriteLine("Count: {0}"?? parts.Count);
parts.TrimExcess();
Console.WriteLine(" TrimExcess()");
Console.WriteLine("Capacity: {0}"?? parts.Capacity);
Console.WriteLine("Count: {0}"?? parts.Count);
parts.Clear();
Console.WriteLine(" Clear()");
Console.WriteLine("Capacity: {0}"?? parts.Capacity);
Console.WriteLine("Count: {0}"?? parts.Count);
Console.Read();
}
}
public class Part
{
public string PartName { get; set; }
public int PartId { get; set; }
public override string ToString()
{
return "ID: " + PartId + "   Name: " + PartName;
}
}
?????????list??Capacity??TrimExcess()??????????????????????????????????????У?????????????????????
????1.??????????????List????????????????Item????????????????List<T>????Capacity??????????????List???????public List(int capacity);?????
????2.???????????list??remove??????????????list????????????????????????????????????????????TrimExcess???????????????檔
?????????????list??????????????????????????????????????