Generic List
Explore how to create and manipulate generic lists using the List<T> class in C#. Understand operations like adding, removing, and sorting elements while ensuring type safety. Learn about List capacity, performance implications, and how to protect internal data with IReadOnlyList<T> for safer software design.
While the ArrayList class solved the fixed-size limitations of arrays, it introduced type safety problems. .NET developers addressed the concerns with a set of generic collection classes.
This lesson focuses on the List<T> class. Instead of using an object[] type array like the ArrayList class does, generic collections use parameter types. In the case of List<T>, the internal array is of type T[].
Generic collections introduce strong typing. The List<int> class can only store int items, and List<string> can only store string objects. We avoid the casting issues we had with the non-generic ArrayList collection.
Syntax
The List<T> class is instantiated just like any other class:
var listOfIntegers = new List<int>();
Here, we create a new instance of a generic list that can only store integers. The internal array is of type int[].
Note: In modern C#, we can initialize collections using collection expressions, such as List<int> listOfIntegers = [];. We will use this modern syntax in our upcoming examples.
Lists we create are initially empty. Empty means that the size of the internal array is zero. When we add the first item, though, a new array of size four is created.
listOfIntegers.Add(17);
Here, we use the Add method to append the integer 17 to the collection.
Each time this array fills, it doubles in size, and values from the previous array are copied over.
Understanding this resizing behavior helps optimize memory. If we know exactly how many items we plan to store, we can set the initial capacity of the list:
var listOfIntegers = new List<int>(500);
Here, we instantiate ...