What is a list?

Lists and arrays are similar in that they both hold items. Lists can easily search, sort, add, and remove items because of built-in functionalities. Lists can constantly be changed to include or disclude additional items, unlike an array that has a set number of items.

To use a list, add the following line of code.

using System.Collections.Generic; // C# 10 comes with ImplicitUsings, meaning the most popular using namespaces such as this one is already built-in so there is no need to declare it

The code above gives access to the Generic namespace that includes the generic list class. Because the example is using generics, we specify the data type of the list class, such as string, int, or double.

Instantiate and assign items

Let’s create a list object class and add some items to it.

Get hands-on with 1200+ tech skills courses.