我想清空一张清单。如何做到这一点?
发布于 2011-03-15 11:37:23
这真的很简单:
myList.Clear();
发布于 2011-03-15 11:38:04
如果"list“指的是List<T>
,那么Clear方法就是您想要的:
List<string> list = ...;
...
list.Clear();
您应该养成搜索有关这些内容的MSDN文档的习惯。
下面是如何快速搜索有关该类型各种位的文档:
List<T>
Count的文档
所有这些谷歌查询都列出了一系列链接,但通常你会选择谷歌在每种情况下给你的第一个链接。
发布于 2016-06-22 18:26:35
选项#1:使用Clear()函数清空List<T>
并保留其容量。
选项#2 -使用Clear()和TrimExcess()函数将List<T>
设置为初始状态。
List<T>
会将列表的容量设置为默认容量。定义
Count = List<T>
中实际存在的元素数量
Capacity =内部数据结构在不调整大小的情况下可以容纳的元素总数。
仅限Clear()
List<string> dinosaurs = new List<string>();
dinosaurs.Add("Compsognathus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("Deinonychus");
Console.WriteLine("Count: {0}", dinosaurs.Count);
Console.WriteLine("Capacity: {0}", dinosaurs.Capacity);
dinosaurs.Clear();
Console.WriteLine("\nClear()");
Console.WriteLine("\nCount: {0}", dinosaurs.Count);
Console.WriteLine("Capacity: {0}", dinosaurs.Capacity);
Clear()和TrimExcess()
List<string> dinosaurs = new List<string>();
dinosaurs.Add("Triceratops");
dinosaurs.Add("Stegosaurus");
Console.WriteLine("Count: {0}", dinosaurs.Count);
Console.WriteLine("Capacity: {0}", dinosaurs.Capacity);
dinosaurs.Clear();
dinosaurs.TrimExcess();
Console.WriteLine("\nClear() and TrimExcess()");
Console.WriteLine("\nCount: {0}", dinosaurs.Count);
Console.WriteLine("Capacity: {0}", dinosaurs.Capacity);
https://stackoverflow.com/questions/5311124
复制相似问题