C# - List
The List<T> is a collection of strongly typed objects that can be accessed by index and having methods for sorting, searching, and modifying list. It is the generic version of the ArrayList that comes under System.Collections.Generic namespace.
List<T> Characteristics
List<T>equivalent of the ArrayList, which implements IList<T>.- It comes under
System.Collections.Genericnamespace. List<T>can contain elements of the specified type. It provides compile-time type checking and doesn't perform boxing-unboxing because it is generic.- Elements can be added using the
Add(),methods or collection-initializer syntax.AddRange() - Elements can be accessed by passing an index e.g.
myList[0]. Indexes start from zero. List<T>performs faster and less error-prone than theArrayList.
Creating a List
The List<T> is a generic collection, so you need to specify a type parameter for the type of data it can store. The following example shows how to create list and add elements.
List<int> primeNumbers = new List<int>();
primeNumbers.Add(2); // adding elements using add() method
primeNumbers.Add(3);
primeNumbers.Add(5);
primeNumbers.Add(7);
var cities = new List<string>();
cities.Add("New York");
cities.Add("London");
cities.Add("Mumbai");
cities.Add("Chicago");
cities.Add(null);// nulls are allowed for reference type list
//adding elements using collection-initializer syntax
var bigCities = new List<string>()
{
"New York",
"London",
"Mumbai",
"Chicago"
};
In the above example, List<int> primeNumbers = new List<int>(); creates a list of int type. In the same way, cities and bigCities are string type list. You can then add elements in a list using the Add() method or the collection-initializer syntax.
You can also add elements of the custom classes using the collection-initializer syntax. The following adds objects of the Student class in the List<Student>.
var students = new List<Student>() {
new Student(){ Id = 1, Name="Bill"},
new Student(){ Id = 2, Name="Steve"},
new Student(){ Id = 3, Name="Ram"},
new Student(){ Id = 4, Name="Abdul"}
};Accessing a List
A list can be accessed by an index, a for/foreach loop, and using LINQ queries. Indexes of a list start from zero. Pass an index in the square brackets to access individual list items, same as array. Use a foreach or for loop to iterate a List<T> collection.
Example: Accessing ListList<int> numbers = new List<int>() { 1, 2, 5, 7, 8, 10 };
Console.WriteLine(numbers[0]); // prints 1
Console.WriteLine(numbers[1]); // prints 2
Console.WriteLine(numbers[2]); // prints 5
Console.WriteLine(numbers[3]); // prints 7
// using foreach LINQ method
numbers.ForEach(num => Console.WriteLine(num + ", "));//prints 1, 2, 5, 7, 8, 10,
// using for loop
for(int i = 0; i < numbers.Count; i++)
Console.WriteLine(numbers[i]);
foreach or for loop to iterate a List<T> collection.List<int> numbers = new List<int>() { 1, 2, 5, 7, 8, 10 };
Console.WriteLine(numbers[0]); // prints 1
Console.WriteLine(numbers[1]); // prints 2
Console.WriteLine(numbers[2]); // prints 5
Console.WriteLine(numbers[3]); // prints 7
// using foreach LINQ method
numbers.ForEach(num => Console.WriteLine(num + ", "));//prints 1, 2, 5, 7, 8, 10,
// using for loop
for(int i = 0; i < numbers.Count; i++)
Console.WriteLine(numbers[i]);
Accessing a List using LINQ
The List<T> implements the IEnumerable interface. So, we can query a list using LINQ query syntax or method syntax, as shown below.
Example: LINQ Query on Listvar students = new List<Student>() {
new Student(){ Id = 1, Name="Bill"},
new Student(){ Id = 2, Name="Steve"},
new Student(){ Id = 3, Name="Ram"},
new Student(){ Id = 4, Name="Abdul"}
};
//get all students whose name is Bill
var result = from s in students
where s.Name == "Bill"
select s;
foreach(var student in result)
Console.WriteLine(student.Id + ", " + student.Name);
List<T> implements the IEnumerable interface. So, we can query a list using LINQ query syntax or method syntax, as shown below.var students = new List<Student>() {
new Student(){ Id = 1, Name="Bill"},
new Student(){ Id = 2, Name="Steve"},
new Student(){ Id = 3, Name="Ram"},
new Student(){ Id = 4, Name="Abdul"}
};
//get all students whose name is Bill
var result = from s in students
where s.Name == "Bill"
select s;
foreach(var student in result)
Console.WriteLine(student.Id + ", " + student.Name);List<T> Class Properties and Methods
The following table lists the important properties and methods of List<T> class:
| Property | Usage |
|---|---|
| Items | Gets or sets the element at the specified index |
| Count | Returns the total number of elements exists in the List<T> |
| Method | Usage |
|---|---|
| Add | Adds an element at the end of a List<T>. |
| AddRange | Adds elements of the specified collection at the end of a List<T>. |
| BinarySearch | Search the element and returns an index of the element. |
| Clear | Removes all the elements from a List<T>. |
| Contains | Checks whether the specified element exists or not in a List<T>. |
| Find | Finds the first element based on the specified predicate function. |
| Foreach | Iterates through a List<T>. |
| Insert | Inserts an element at the specified index in a List<T>. |
| InsertRange | Inserts elements of another collection at the specified index. |
| Remove | Removes the first occurrence of the specified element. |
| RemoveAt | Removes the element at the specified index. |
| RemoveRange | Removes all the elements that match the supplied predicate function. |
| Sort | Sorts all the elements. |
| TrimExcess | Sets the capacity to the actual number of elements. |
| TrueForAll | Determines whether every element in the List<T> matches the conditions defined by the specified predicate. |
Comments
Post a Comment