I want to empty a list. How to do that?
Option #1: Use Clear() function to empty the List and retain it's capacity.
Count is set to 0, and references to other objects from elements of the collection are also released.
Capacity remains unchanged.
Option #2 - Use Clear() and TrimExcess() functions to set List to initial state.
Count is set to 0, and references to other objects from elements of the collection are also released.
Trimming an empty
Listsets the capacity of the List to the default capacity.
Definitions
Count = number of elements that are actually in the List
Capacity = total number of elements the internal data structure can hold without resizing.
Clear() Only
List dinosaurs = new List();
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()
List dinosaurs = new List();
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);