C# List class represents a collection of strongly typed objects that can be accessed by index. This tutorial teaches how to work with lists in C# using the C# List class to add, find, sort, reverse, and search items in a collection of objects using List class methods and properties.
What is C# List?
List<T> class in C# represents a strongly typed list of objects. The C# List provides functionality to create a list of objects, add items to a list, and find, sort, and update items in the List. In this article, learn how to create a list in C#, add items to a list, and find and remove items to a list.
Create a List in C#
C# List is a generic class and is defined in the System.Collections.Generic namespace. You must import this namespace in your project to access the List <T>, class.
using System.Collections.Generic;
List<T> class constructor is used to create a List object of type T. It can either be empty or take an Integer value as an argument that defines the initial size of the List, also known as capacity. If no integer is passed in the constructor, the size of the List is dynamic and grows every time an item is added to the array. You can also give an initial collection of elements when initializing an object.
The code snippet in Listing 1 creates a List of Int16 and a list of string types. The last part of the code creates a List<T> object with an existing collection.
// List with default capacity
List<Int16> list = new List<Int16>();
// List with capacity = 5
List<string> authors = new List<string>(5);
string[] animals = { "Cow", "Camel", "Elephant" };
List<string> animalsList = new List<string>(animals);
Listing 1.
As you can see from Listing 1, the List <string> has only an initial capacity set to 5. However, when more than five elements are added to the List, it automatically expands.
Add an item to a C# List
The Add method adds an element to a C# List. For example, the code snippet in Listing 2 creates two List <T> objects and adds integer and string items.
// Dynamic ArrayList with no size limit
List<int> numberList = new List<int>();
numberList.Add(32);
numberList.Add(21);
numberList.Add(45);
numberList.Add(11);
numberList.Add(89);
// List of string
List<string> authors = new List<string>(5);
authors.Add("Mahesh Chand");
authors.Add("Chris Love");
authors.Add("Allen O'neill");
authors.Add("Naveen Sharma");
authors.Add("Monica Rathbun");
authors.Add("David McCarter");
Listing 2.
We can also add a collection to a List. The AddRange method is used to add a group to a List. For example, the code snippet in Listing 3 adds an array of strings to a List.
// Collection of string
string[] animals = { "Cow", "Camel", "Elephant" };
// Create a List and add a collection
List<string> animalsList = new List<string>();
animalsList.AddRange(animals);
foreach (string a in animalsList)
Console.WriteLine(a);
Listing 3.
Loop through a C# List items
The C# List is a collection of items. We can use a foreach loop to loop through its items. The code snippet in Listing 6 reads all list items and displays them on the console.
foreach (string a in authors)
Console.WriteLine(a);
Listing 4.
We can use the collection's index to retrieve an item in a C# List at a specific position. For example, the following code snippet reads the 3rd item in the List.
Console.WriteLine(authors[2]);
C# List properties
List class properties include the following:
- Capacity – Number of elements List<T> can contain. The default capacity of a List<T> is 0.
- Count – Number of elements in List <T>.
The code snippet in Listing 5 gets the value of these properties.
ArrayList authorsArray = new ArrayList();
authorsArray.Add("Mahesh Chand");
authorsArray.Add("Praveen Kumar");
authorsArray.Add("Raj Kumar");
authorsArray.Add("Dinesh Beniwal");
authorsArray.Add("David McCarter");
Console.WriteLine("Count: " + authors.Count);
Console.WriteLine("Capacity: " + authors.Capacity);
Listing 5.
Insert an item at a position in a C# List
The Insert method of the List class inserts an object at a given position. The first parameter of the method is the 0th-based index in the List.
The InsertRange method can insert a collection at the given position.
The code snippet in Listing six inserts a string at the 3rd position and an array at the 2nd position of the List <T>.
authors.Insert(3, "Bill Author");
// Collection of new authors
string[] newAuthors = { "New Author1", "New Author2", "New Author3" };
// Insert array at position 2
authors.InsertRange(2, newAuthors);
Listing 6.
Remove an item from a C# List
The List class provides Remove methods that can be used to remove an item or a range of items.
The Remove method removes the first occurrence of the given item in the List. For example, the following code snippet removes the first occurrence of 'New Author1'.
// Remove an item
authors.Remove("New Author1");
The RemoveAt method removes an item at the given position. For example, the following code snippet removes the item at the 3rd position.
// Remove 3rd item
authors.RemoveAt(3);
The RemoveRange method removes a list of items from the starting index to the number of items. For example, the following code snippet removes two items starting at the 3rd position.
// Remove a range
authors.RemoveRange(3, 2);
The Clear method removes all items from a List<T>. For example, the following code snippet removes all items from a List.
// Remove all items
authors.Clear();
Clear all items from a C# List
The Clear method removes all items from a C# List. For example, the following code snippet removes all items from a List.
// Remove all items
authors.Clear();
Check if an item exists in a C# List
The IndexOf method finds an item in a List. The IndexOf method returns -1 if no items are located in the List.
The following code snippet finds a string and returns the matched position of the item.
int idx = authors.IndexOf("Naveen Sharma");
if (idx > 0)
Console.WriteLine($"Item index in List is: {idx}");
else
Console.WriteLine("Item not found");
We can also specify the position in a List from which the IndexOf method can start searching.
For example, the following code snippet finds a string starting at the 3rd position in a String.
Console.WriteLine(authors.IndexOf("Naveen Sharma", 2));
The LastIndexOf method finds an item from the end of the List.
The following code snippet looks for a string in the backward direction and returns the index of the item if found.
Console.WriteLine(authors.LastIndexOf("Mahesh Chand"));
The complete example is listed in Listing 7.
// List of string
List<string> authors = new List<string>(5);
authors.Add("Mahesh Chand");
authors.Add("Chris Love");
authors.Add("Allen O'neill");
authors.Add("Naveen Sharma");
authors.Add("Mahesh Chand");
authors.Add("Monica Rathbun");
authors.Add("David McCarter");
int idx = authors.IndexOf("Naveen Sharma");
if (idx > 0)
Console.WriteLine($"Item index in List is: {idx}");
else
Console.WriteLine("Item not found");
Console.WriteLine(authors.IndexOf("Naveen Sharma", 2));
Console.WriteLine(authors.LastIndexOf("Mahesh Chand"));
Listing 7.
Sort a C# List
The Sort method of List <T> sorts all items of the List using the QuickSort algorithm.
The following code example in Listing eight sorts List items and displays both the original and sorted order of the List items.
// List of string
List<string> authors = new List<string>(5);
authors.Add("Mahesh Chand");
authors.Add("Chris Love");
authors.Add("Allen O'neill");
authors.Add("Naveen Sharma");
authors.Add("Mahesh Chand");
authors.Add("Monica Rathbun");
authors.Add("David McCarter");
Console.WriteLine("Original List items");
Console.WriteLine("===============");
// Print original order
foreach (string a in authors)
Console.WriteLine(a);
// Sort list items
authors.Sort();
Console.WriteLine();
Console.WriteLine("Sorted List items");
Console.WriteLine("===============");
// Print sorted items
foreach (string a in authors)
Console.WriteLine(a);
Listing 8.
The output of Listing eight looks like Figure 2.
Figure 2.
Reverse a C# List
The Reverse method of List <T> reverses the order of all items in the List.
The following code snippet reverses a List.
// List of string
List<string> authors = new List<string>(5);
authors.Add("Mahesh Chand");
authors.Add("Chris Love");
authors.Add("Allen O'neill");
authors.Add("Naveen Sharma");
authors.Add("Mahesh Chand");
authors.Add("Monica Rathbun");
authors.Add("David McCarter");
Console.WriteLine("Original List items");
Console.WriteLine("===============");
// Print original order
foreach (string a in authors)
Console.WriteLine(a);
// Reverse list items
authors.Reverse();
Console.WriteLine();
Console.WriteLine("Sorted List items");
Console.WriteLine("===============");
// Print reversed items
foreach (string a in authors)
Console.WriteLine(a);
Listing 9.
The output of Listing nine looks like Figure 3.
Figure 3.
Find an Item in a C# List
The BinarySearch method of List <T> searches a sorted list and returns the zero-based index of the found item. The List <T> must be sorted before this method can be used.
The following code snippet returns an index of a string in a List.
int bs = authors.BinarySearch("Mahesh Chand");
Import items to a C# List from another List
You can use the AddRange method of List to import items from one List into another list. But make sure the item types are the same in both lists. For example, the following code snippet creates two List objects and copies all items of listTwo into listOne.
// Program: Copy items from one list to another list
Console.WriteLine("Import one list to another!");
// Create List1
List<string> listOne = new();
listOne.Add("One");
listOne.Add("Two");
listOne.Add("Three");
listOne.Add("Four");
listOne.Add("Five");
// Create List2
List<string> listTwo = new();
listTwo.Add("A");
listTwo.Add("B");
listTwo.Add("C");
// Add List2 to List1
listOne.AddRange(listTwo);
// Display
foreach(string item in listOne)
Console.WriteLine(item);
Console.ReadKey();
Convert a C# List to an array
You can use the ToArray() method of the C# List class to convert a list into an array.
int[] a = number.ToArray();
Join two C# Lists
You can use the AddRange method to merge a C# List with an existing C# List. Here is a detailed article on How to Merge Two C# Lists.
List1.AddRange(List2);
Summary
This article demonstrated how to use a List<T> class to work with a collection of objects. The article also showed how to add, find, search, sort, and reverse items in a List.