List In C#-1
List In C#-1
C# Corner Login
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.
1 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.
Listing 1.
As you can see from Listing 1, the List <string> has only an initial capacity set to 5. However, when more
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.
1 // Collection of string
2 string[] animals = { "Cow", "Camel", "Elephant" };
3 // Create a List and add a collection
4 List<string> animalsList = new List<string>();
5 animalsList.AddRange(animals);
6 foreach (string a in animalsList)
7 Console.WriteLine(a);
Listing 3.
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.
1 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>.
Listing 5.
The code snippet in Listing six inserts a string at the 3rd position and an array at the 2nd position of the List
<T>.
Listing 6.
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'.
1 // Remove an item
2 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.
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.
1 // Remove a range
2 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.
The following code snippet finds a string and returns the matched position of the item.
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.
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.
1 Console.WriteLine(authors.LastIndexOf("Mahesh Chand"));
1 // List of string
2 List<string> authors = new List<string>(5);
3 authors.Add("Mahesh Chand");
4 authors.Add("Chris Love");
5 authors.Add("Allen O'neill");
6 authors.Add("Naveen Sharma");
7 authors.Add("Mahesh Chand");
8 authors.Add("Monica Rathbun");
9 authors.Add("David McCarter");
10 int idx = authors.IndexOf("Naveen Sharma");
11 if (idx > 0)
12 Console.WriteLine($"Item index in List is: {idx}");
13 else
14 Console.WriteLine("Item not found");
15 Console.WriteLine(authors.IndexOf("Naveen Sharma", 2));
16 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.
1 // List of string
2 List<string> authors = new List<string>(5);
3 authors.Add("Mahesh Chand");
4 authors.Add("Chris Love");
5 authors.Add("Allen O'neill");
6 authors.Add("Naveen Sharma");
7 authors.Add("Mahesh Chand");
8 authors.Add("Monica Rathbun");
9 authors.Add("David McCarter");
10 Console.WriteLine("Original List items");
11 Console.WriteLine("===============");
12 // Print original order
13 foreach (string a in authors)
14 Console.WriteLine(a);
15 // Sort list items
16 authors.Sort();
17 Console.WriteLine();
18 Console.WriteLine("Sorted List items");
19 Console.WriteLine("===============");
20 // Print sorted items
21 foreach (string a in authors)
22 Console.WriteLine(a);
Listing 8.
Figure 2.
Reverse a C# List
The Reverse method of List <T> reverses the order of all items in the List.
1 // List of string
Listing 9.
Figure 3.
1 int[] a = number.ToArray();
1 List1.AddRange(List2);