如何在C#中清空列表?
我想清空一个列表。如何做到这一点?非常简单:如何在C#中清空列表?,c#,.net,C#,.net,我想清空一个列表。如何做到这一点?非常简单: myList.Clear(); 你能做到的 var list = new List<string>(); list.Clear(); var list=newlist(); list.Clear(); 如果“列表”是指列表,则该方法就是您想要的: List<string> list = ...; ... list.Clear(); List=。。。; ... list.Clear(); 您应该养成在MSDN文档中搜索这
myList.Clear();
你能做到的
var list = new List<string>();
list.Clear();
var list=newlist();
list.Clear();
如果“列表”是指列表,则该方法就是您想要的:
List<string> list = ...;
...
list.Clear();
List=。。。;
...
list.Clear();
您应该养成在MSDN文档中搜索这些内容的习惯
下面介绍了如何快速搜索该类型各种位的文档:
- -提供
列表
类本身(这是您应该开始的地方)
- -提供有关方法的文档
- -提供有关财产计数的文档
所有这些Google查询都列出了一组链接,但通常情况下,您需要Google在每种情况下提供给您的第一个链接。您可以使用clear方法
List<string> test = new List<string>();
test.Clear();
List test=newlist();
test.Clear();
列表中需要Clear()函数,如下所示
List<object> myList = new List<object>();
myList.Add(new object()); // Add something to the list
myList.Clear() // Our list is now empty
List myList=new List();
myList.Add(新对象());//在列表中添加一些内容
myList.Clear()//我们的列表现在为空
给出备选答案(谁需要5个相同的答案?):
list.Add(5);
//列表现在至少包含一个元素
列表=新列表();
//“列表”中的列表现在为空
请记住,对旧列表的所有其他引用尚未清除(根据情况,这可能是您想要的)。此外,就性能而言,它通常会稍微慢一些。选项1:使用函数清空列表
,并保留其容量
-
Count设置为0,并从的元素引用其他对象
该系列也将发布
-
容量保持不变
选项2-使用和函数将列表
设置为初始状态
-
Count设置为0,并从的元素引用其他对象
该系列也将发布
-
修剪空的
列表
将列表的容量设置为
默认容量
定义
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);
=实际在列表中的元素数
=内部数据结构在不调整大小的情况下可容纳的元素总数
仅清除()
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);
列出恐龙=新列表();
恐龙。添加(“Compsognatus”);
恐龙。添加(“Amargasaurus”);
恐龙。添加(“Deinonychus”);
WriteLine(“Count:{0}”,digoros.Count);
WriteLine(“容量:{0}”,恐龙.Capacity);
恐龙;
Console.WriteLine(“\nClear()”);
Console.WriteLine(“\n计数:{0}”,恐龙数);
WriteLine(“容量:{0}”,恐龙.Capacity);
清除()和修剪多余()
列出恐龙=新列表();
恐龙。加上(“三角龙”);
恐龙。添加(“剑龙”);
WriteLine(“Count:{0}”,digoros.Count);
WriteLine(“容量:{0}”,恐龙.Capacity);
恐龙;
恐龙;
Console.WriteLine(“\nClear()和trimOverse()”);
Console.WriteLine(“\n计数:{0}”,恐龙数);
WriteLine(“容量:{0}”,恐龙.Capacity);
…如果列表实际上是列表
,数组列表
,或实现IList
)@Lucero如果两者都不是,它会被称为一个列表吗?因为这是谷歌的热门话题,我遇到了这个问题,我在necro发表评论。如果在循环中使用同一个列表并使用clear,该列表通常会保留对旧对象的引用-我经常使用=new list();因为它会立即清除所有旧的分配。但对大多数人来说,.Clear();这就足够了,但如果您发现列表的行为异常,请尝试使用=new list();。我将对此进行双重评论,因为截至2020年,此处提供了一些错误信息。List.Clear
正确清除所有引用,允许GC在必要时清除alloc新建列表
不能做到这一点,而且在所有场景中都不适用。此处的响应时间比读取/搜索MSDNPlus要快。现在,在此处找到答案比在MSDN之前更容易。更不用说,如果您使用Google.net空列表
,此页面会在MSDN之前出现@V4Vendetta在2018年,我建议StackOverflow可能应该创建一个机器人,将您的评论放在评论中有指向MSDN doc链接的任何线程上……”所有这些都列出了一组链接,但通常您想要第一个。“除非您想清空列表?对不起,我应该更清楚一些。通常,您需要的是Google提供给您的第一个链接,而不是“List类”。您能否验证设置List=new List()实际上比List.Clear()慢;?根据MSDN(下面的链接)列表。清除();是一个O(N)操作,我无法想象实例化一个新列表所需的时间会比这个长。DotNetPerls做了一个基准测试,发现新列表更快。此外,还要注意,列表
有一个新的引用。因此,如果你在列表上使用锁,就不要使用它。@GerhardPowell根据你提供的文章,与使用Clear()
方法相比,创建一个新的列表不再快了在结果中,对列表调用Clear()更快。
Clear()将手动清除列表(O(n)),new List将删除对旧列表的引用,并使GC清除旧列表(显然也是O(n))。因此,在需要垃圾收集之前,新列表的实例化速度似乎更快——在这种情况下,重用旧列表(清除)变得更有效。
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);