简体   繁体   English

如何在C#中清空列表?

[英]How to empty a list in C#?

I want to empty a list. 我想清空列表。 How to do that? 怎么做?

真的很简单:

myList.Clear();

If by "list" you mean a List<T> , then the Clear method is what you want: 如果用“列表”表示List<T> ,那么Clear方法就是您想要的:

List<string> list = ...;
...
list.Clear();

You should get into the habit of searching the MSDN documentation on these things. 您应该养成在这些方面搜索MSDN文档的习惯。

Here's how to quickly search for documentation on various bits of that type: 以下是快速搜索有关该类型各个位的文档的方法:

All of these Google queries lists a bundle of links, but typically you want the first one that google gives you in each case. 所有这些Google查询都会列出一堆链接,但通常情况下,您都希望google提供给您的第一个链接。

Option #1: Use Clear() function to empty the List<T> and retain it's capacity. 选项#1:使用Clear()函数清空List<T>并保留其容量。

  • Count is set to 0, and references to other objects from elements of the collection are also released. Count设置为0,并且还释放对集合元素中其他对象的引用。

  • Capacity remains unchanged. 容量保持不变。

Option #2 - Use Clear() and TrimExcess() functions to set List<T> to initial state. 选项#2-使用Clear()TrimExcess()函数将List<T>设置为初始状态。

  • Count is set to 0, and references to other objects from elements of the collection are also released. Count设置为0,并且还释放对集合元素中其他对象的引用。

  • Trimming an empty List<T> sets the capacity of the List to the default capacity. 修剪空的List<T>会将List<T>的容量设置为默认容量。

Definitions 定义

Count = number of elements that are actually in the List<T> Count = List<T>中实际存在的元素数

Capacity = total number of elements the internal data structure can hold without resizing. 容量 =内部数据结构无需调整大小即可容纳的元素总数。

Clear() Only 仅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() and TrimExcess() 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);

To give an alternative answer (Who needs 5 equal answers?): 给出替代答案(谁需要5个相等的答案?):

list.Add(5); 
// list contains at least one element now
list = new List<int>();
// list in "list" is empty now

Keep in mind that all other references to the old list have not been cleared (depending on the situation, this might be what you want). 请记住,对旧列表的所有其他引用都尚未清除(取决于情况,这可能是您想要的)。 Also, in terms of performance, it is usually a bit slower. 而且,在性能方面,它通常会慢一些。

you can do that 你可以做到

var list = new List<string>();
list.Clear();

You can use the clear method 您可以使用清除方法

List<string> test = new List<string>();
test.Clear();

You need the Clear() function on the list, like so. 像这样,您需要列表上的Clear()函数。

List<object> myList = new List<object>();

myList.Add(new object()); // Add something to the list

myList.Clear() // Our list is now empty

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM