.NET Collections - Part Two - Generic Collections List<T>

List is a generic collection that is defined in the System.Collection.Generic namespace. List<T> is used to store a collection elements or objects in the form of a list. It reflects a strongly typed index-accessible list of objects. The List class provides functionality to search, sort, and manipulate list elements. It offers the same features as ArrayList, but the only distinction is that-list is a generic collection while ArrayList is a non-generic collection.
 
The size of the list increases as needed. The ICollection < T >, IEnumerable < T >, IList < T >, IReadOnlyCollection < T >, IReadOnlyList < T >, ICollection, IEnumerable, and IList interface are implemented in the List class.
 
It can recognize null as a valid reference type value and enables duplicate components as well. If the count becomes equivalent to the capability, the size of the list will automatically increase by reallocating the inner array.
 
Before adding the new element, the current components will be transferred to the fresh array. The elements in the list are not sorted by default and zero-based index access to the elements.
 
A List class has 3 constructors to generate a list, and the constructors are as follows,
  1. List<T>()
    This constructor is used to create an instance of the List<T> class that is empty and has the default initial capacity.

  2. List<T>(Int32)
    This constructor is used to create an instance of the List<T> class that is empty and has the specified initial capacity.

  3. List<T>(IEnumerable)
    This constructor is used to create an instance of the List<T> class that contains elements copied from the specified collection and has enough capacity to accommodate the number of elements copied.
We can use either of the 3 above constructors depending on the best fit to the requirement to create a new list.
 
Please refer to the below code which shows all the operations we can perform on List<T>.
  1. public class ListsCollection  
  2.    {  
  3.        public static void Main()  
  4.        {  
  5.            // Creating list using List class   
  6.            // and List<T>() Constructor   
  7.            List<int> numberList = new List<int>();  
  8.   
  9.            // Adding elements to List   
  10.            // Using Add() method   
  11.            numberList.Add(3);  
  12.            numberList.Add(5);  
  13.            numberList.Add(10);  
  14.            numberList.Add(8);  
  15.            numberList.Add(15);  
  16.            numberList.Add(66);  
  17.            numberList.Add(99);  
  18.           
  19.            //AddRange(IEnumerable<T>) - Adding multiple elements to List  
  20.           
  21.            List<int> numberList2=new List<int>();    
  22.            numberList2.AddRange(numberList);  
  23.   
  24.            // Accessing elements of numberList   
  25.            // Using foreach loop   
  26.            foreach (int listitem in numberList)  
  27.            {  
  28.                Console.WriteLine(listitem);  
  29.            }  
  30.   
  31.            // Get List count   
  32.            Console.WriteLine($"List count:{numberList.Count}");  
  33.   
  34.            // Remove() method to remove the list item - using list value  
  35.            numberList.Remove(10);  
  36.            Console.WriteLine($"List count:{numberList.Count}");  
  37.   
  38.            // RemoveAt() method to remove the list item - using list index  
  39.            numberList.RemoveAt(4);  
  40.            Console.WriteLine($"List count:{numberList.Count}");  
  41.   
  42.            // RemoveRange() method - Remove range of elements from list  
  43.            numberList.RemoveRange(0, 2);  
  44.            Console.WriteLine($"List count:{numberList.Count}");  
  45.   
  46.            // Clear() method - removes all the items from List  
  47.            numberList.Clear();  
  48.            Console.WriteLine($"List count:{numberList.Count}");  
  49.   
  50.            // Sort method - Sorts listitems  
  51.            numberList.Sort();  
  52.           
  53.            Console.WriteLine("Sorted List:");  
  54.            foreach (int listitem in numberList)  
  55.            {  
  56.                Console.WriteLine(listitem);  
  57.            }  
  58.     }  
  59. }  
Next Recommended Reading .NET Memory Management - Part Two