Lists
Lists are ordered collections that can hold any object. Implements IList<T>
. Allows for duplicate items.
Inherits from System.Collections.Generic implicitly.List<T>
is not thread safe. Use ImmutableList<T>
instead.
For a heterogeneous collection of objects, use List<Object>
.
Other Lists
SortedList<TKey, TValue>
A collection of key/value pairs that are sorted by key and accessible by key or index.
Creating
Declare a new List of strings:
var names = new List<string>Angle brackets are used for Generics.
// or
var names = new List<string> { "eli", "noah" }Declare and initialize (collection initializer).
Accessing
Lists can be indexed:
names[n]
name[^n] // return the nth item from the end of the list.
Properties
.CountReturns
the number of elements in the List.
Methods
Manipulating
.Add(element) // Add element to List.
.AddRange(new[] { elem1, elem2, … }
.Remove(element) // Remove element from List.
Searching
.Contains(element)
.IndexOf(element) // Returns the index of element, or -1 if not found.
Sorting
.Sort() // Sorts the List in place.