C# List
Following examples show how to create and manipulate with .NET strongly typed list List<T>
.
List<T>
List is a generic type, so you can create list of any type (it can be reference type such asCustomer
or value type such as int
)
var list1 = new List<object>(); var list2 = new List<Customer>(); var list3 = new List<int>();
All the following examples use list of integer values List<int>
.
List Constructor
Creates an empty list (of integer values).
var list = new List<int>();
Creates a list and initializes it with some items (integer values).
var list = new List<int>() { 8, 3, 2 };
Creates a list and initializes it with items of another list (or array or anything which implements IEnumerable interface).
var listA = new List<int>() { 8, 3, 2 };
var listB = new List<int>(listA);
Creates a list with specified capacity.
var list = new List<int>(16);
list.Count: |
0 |
list.Capacity: |
16 |
List[index]
Gets an item at the specified zero-based index.
Sets the item at the specified zero-based index.
List.Add
Adds an item to the end of the list.
List.AddRange
Adds items of another list (or an IEnumerable collection) to the end of the list.
List.BinarySearch
Returns the zero-based index of the item in the sorted list. If the items is not found, returns a negative number. See MSDN for more info.
This List<T> method works only if the type T implements IComparable<T> or IComparable interface.
int index = list.BinarySearch(6);
This BinarySearch method overload uses specified comparer.
int index = list.BinarySearch(item: 6,comparer: new MyComparer() );
This BinarySearch method overload uses specified comparer and search in specified range.
int index = list.BinarySearch(index: 1, count: 3, item: 6,comparer: new MyComparer() );
This example shows the case when the item was not found in the list. The result is negative number.
int index = list.BinarySearch(index: 1, count: 2, item: 6,comparer: new MyComparer() );
public class MyComparer : IComparer<int>
{
public int Compare(int x, int y) { return x.CompareTo(y); }
}
List.Clear
Removes all items from the list. Count is zero, Capacity is unchanged.
List.Contains
Returns true if the list contains the specified item.
bool result = list.Contains(3);
Returns false if the list doesn't contain the specified item.
bool result = list.Contains(6);
List.ConvertAll
Converts items using specified delegate. Items can be converted to another type.
var conv = new Converter<int,decimal>(x => (decimal)(x+1));
var listB = listA.ConvertAll<decimal>(conv);
List.CopyTo
Copies all list items into the beginning of the specified array.
list: |
8 3 2 |
array: |
0 0 0 0 0 |
list.CopyTo(array);
Copies all list items into the array, starting from the specified array index.
list: |
8 3 2 |
array: |
0 0 0 0 0 |
list.CopyTo(array, arrayIndex: 2);
Copies a range of list items into the array, starting from the specified array index.
list: |
8 3 2 |
array: |
0 0 0 0 0 |
list.CopyTo(index: 1, array: array,arrayIndex: 3, count: 1);
List.Exists
Returns true if the list contains items matching the specified predicate.
bool result = list.Exists(x => x == 3);
Returns false if the list doesn't contain items matching the specified predicate.
bool result = list.Exists(x => x > 10);
List.Equals
Returns true if the two specified lists are reference equal (are the same instance).
var listA = new List<int>() { 8, 3, 2 };
var listB = listA;
bool result = listA.Equals(listB);
Returns false if the specified two lists are not the same instance. To determine whether all items of the two lists are equal use LINQ method SequenceEqual.
var listA = new List<int>() { 8, 3, 2 };
var listB = new List<int>() { 8, 3, 2 };
bool result = listA.Equals(listB);
List.Find
Returns the first occurrence of item matching the specified predicate.
int item = list.Find(x => x > 2);
For List<T> returns the default value of type T if no item matches the predicate. In this case the default value of int is 0.
int item = list.Find(x => x > 10);
List.FindAll
Returns list with items matching the specified predicate.
var listB = listA.FindAll(x => x > 2);
Returns empty list, if no item matches the specified predicate.
var listB = listA.FindAll(x => x > 10);
List.FindIndex
Returns zero-based index of the first item which matches the specified predicate.
int index = list.FindIndex(x => x < 5);
Returns index of the first item which matches the specified predicate. It searches the list from startIndex to the end of the list.
int index = list.FindIndex(startIndex: 2, match: x => x < 5);
Returns index of the first item which matches the specified predicate. It searches the list in the range specified by startIndex and count.
int index = list.FindIndex(startIndex: 2, count: 2,match: x => x < 5);
Returns -1 if no item matches the specified predicate.
int index = list.FindIndex(startIndex: 2, count: 2,match: x => x < 3);
List.FindLast
Returns the last occurrence of item matching the specified predicate.
int item = list.FindLast(x => x < 5);
For List<T> returns the default value of type T if no item matches the predicate. In this case the default value of int is 0.
int item = list.FindLast(x => x > 10);
List.FindLastIndex
Returns zero-based index of the last item which matches the specified predicate.
int index = list.FindLastIndex(x => x < 5);
Returns index of the last item which matches the specified predicate. It searches the list from the beginning to the specified startIndex.
int index = list.FindLastIndex(startIndex: 2, match: x => x < 5);
Returns index of the last item which matches the specified predicate in the range specified by count and the end index surprisingly called startIndex.
int index = list.FindLastIndex(startIndex: 2, count: 2,match: x => x < 5);
Returns -1 if no item matches the specified predicate.
int index = list.FindLastIndex(startIndex: 2, count: 2,match: x => x < 3);
List.ForEach
Executes the specified action for each item in the list. It does the same as standardC# foreach statement.
list.ForEach(x => {Console.Write(x); });
List.GetRange
Returns a list with a range of items of the source list.
var listB = listA.GetRange(index: 1, count: 3);
List.IndexOf
Returns the zero-based index of the first occurrence of the item in the list.
int index = list.IndexOf(8);
Returns the index of the first occurrence of the item in the list. It searches the list from the specified index to the end of the list.
int index = list.IndexOf(item: 8,index: 1);
Returns the index of the first occurrence of the item in the list. It searches the list in the range specified by index and count.
int index = list.IndexOf(item: 3,index: 1, count: 2);
Returns -1 if the item is not found in the specified range.
int index = list.IndexOf(item: 8,index: 1, count: 2);
List.Insert
Inserts an item into the list at the specified index.
list.Insert(index: 1, item: 5);
List.InsertRange
Inserts items of another list (or object implementing IEnumerable) into the list at the specified index.
listA.InsertRange(index: 1,collection: listB);
List.LastIndexOf
Returns the zero-based index of the last occurrence of the item in the list.
int index = list.LastIndexOf(8);
Returns the index of the last occurrence of the item in the list. It searches the list from the beginning of the list to the specified index.
int index = list.LastIndexOf(item: 8, index: 3);
Returns the index of the last occurrence of the item in the list. It searches in the range specified by count and the end index.
int index = list.LastIndexOf(item: 6, index: 3, count: 2);
Returns -1 if the item is not found in the specified range.
int index = list.LastIndexOf(item: 8, index: 3, count: 2);
List.Remove
Removes the first occurence of the specified item from the list.
List.RemoveAll
Removes all items matching the specified predicate.
list.RemoveAll(x => x < 4);
List.RemoveAt
Removes the item at the specified index.
List.RemoveRange
Removes items from the specified range.
list.RemoveRange(index: 2,count: 3);
List.Reverse
Reverses the order of all items in the list.
Reverses the order of the items in the specified range.
list.Reverse(index: 1, count: 2);
List.Sort
Sorts all items in the list.
This List<T> method works only if the type T implements IComparable<T> or IComparable interface.
Sorts list using comparison delegate.
list.Sort((x, y) => x.CompareTo(y));
Sorts list using comparison delegate (in descending order).
list.Sort((x, y) => y.CompareTo(x));
Sorts the list using custom comparer.
list.Sort(new MyComparer());
Sorts the specified range of the list using custom comparer.
list.Sort(index: 2, count: 3,comparer: new MyComparer());
public class MyComparer : IComparer<int>
{
public int Compare(int x, int y) { return x.CompareTo(y); }
}
List.ToArray
Creates new array and copies all items into it.
int[] array = list.ToArray();
Returns empty array for empty list.
int[] array = list.ToArray();
List.TrimExcess
Trims the list capacity to reduce memory usage if it's reasonable. It sets Capacity to the same value as Count but only if the Count is less than about 90 % of Capacity.
list.Count: |
5 |
list.Capacity: |
8 |
list.TrimExcess();
list.Count: |
5 |
list.Capacity: |
5 |
If the Count is almost the same as the list Capacity it does nothing.
list.Count: |
7 |
list.Capacity: |
8 |
list.TrimExcess();
list.Count: |
7 |
list.Capacity: |
8 |
List.TrueForAll
Returns true if all items in the list match the specified predicate.
bool result = list.TrueForAll(x => x < 10);
Returns false if not all items in the list match the specified predicate.
bool result = list.TrueForAll(x => x < 5);