List Methods and Properties in C#
Lists in C# (List<T>) provide several useful methods and properties for managing and manipulating collections of elements.
Properties
Count
The Count property returns the number of elements currently in the list.
List<int> numbers = new List<int> { 10, 20, 30 };
int count = numbers.Count; // Returns 3
Capacity
The Capacity property indicates the total number of elements the list can hold before needing to resize.
List<int> numbers = new List<int>(10); // Initialized with a capacity of 10
int capacity = numbers.Capacity; // Returns 10
IsReadOnly
The IsReadOnly property indicates whether the list is read-only, meaning its elements cannot be modified.
List<int> numbers = new List<int> { 10, 20, 30 };
bool isReadOnly = numbers.IsReadOnly; // Returns false (default)
Methods
Add and AddRange
You can add elements to a list with the Add method:
List<int> numbers = new List<int>();
numbers.Add(60); // Adds the number 60 to the end of the list
// You can also add multiple elements at once with the AddRange method:
numbers.AddRange(new int[] { 70, 80, 90 }); // Adds the numbers 70, 80, and 90 to the end of the list
To add multiple elements to a list via AddRange:
List<int> numbers = new();
numbers.AddRange(new int[]{ 30, 40, 50 });
Insert
The Insert method allows adding an element at a specific position in the list, shifting the subsequent elements to the right.
List<int> numbers = new List<int> { 10, 20, 30 };
numbers.Insert(1, 15); // Inserts the number 15 at index 1
// List result: { 10, 15, 20, 30 }
Contains
The Contains method checks if a specific element exists in the list.
Returns true if the element is present, or false otherwise.
List<int> numbers = new List<int> { 10, 20, 30 };
bool has20 = numbers.Contains(20); // Returns true
bool has50 = numbers.Contains(50); // Returns false
IndexOf
The IndexOf method returns the index of the first occurrence of an element in the list.
If the element doesn't exist, it returns -1.
List<int> numbers = new List<int> { 10, 20, 30, 20 };
int index1 = numbers.IndexOf(20); // Returns 1 (first 20 found)
int index2 = numbers.IndexOf(50); // Returns -1 (it doesn't exist in the list)
Element Access
You can access list elements by index, just like with arrays:
List<int> numbers = new List<int> { 10, 20, 30 };
int firstNumber = numbers[0]; // Accesses the first element (10)
Find and FindAll
You can search for elements in a list using the Find method, which returns the first element that meets the specified condition:
List<int> numbers = new List<int> { 10, 20, 30 };
int result = numbers.Find(number => number > 20); // Finds the first element greater than 20
To find all elements that meet a condition, you can use the FindAll method:
List<int> numbers = new List<int> { 10, 20, 30 };
List<int> results = numbers.FindAll(number => number > 20); // Finds all elements greater than 20
Modifying elements in a list
You can modify list elements by accessing them through their index:
List<int> numbers = new List<int> { 10, 20, 30 };
numbers[0] = 100; // Changes the first element from 10 to 100
Sort
You can sort the elements of a list using the Sort method:
List<int> numbers = new List<int> { 30, 10, 20 };
numbers.Sort(); // Sorts the list in ascending order (10, 20, 30)
Remove, RemoveAt, and RemoveAll
You can remove elements from a list with the Remove method (which removes the first occurrence of a specified value):
List<int> numbers = new List<int> { 10, 20, 30 };
numbers.Remove(30); // Removes the first occurrence of the value 30
You can also remove elements by their index using the RemoveAt method:
List<int> numbers = new List<int> { 10, 20, 30 };
numbers.RemoveAt(1); // Removes the element at index 1 (20)
To remove all elements in the list that meet a condition, you can use the RemoveAll method:
List<int> numbers = new List<int> { 10, 20, 30, 40, 50 };
numbers.RemoveAll(number => number > 30); // Removes all elements greater than 30