public class List<T>: IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
Object
List<T>This type implements ICollection, IEnumerable, System.Collections.IList<T>, ICollection<T>, IEnumerable<T>, and IList<T>.
mscorlib
BCL
Implements the IList<T> interface. The size of a List is dynamically increased as required. A List is not guaranteed to be sorted. It is the programmer's responsibility to sort the List prior to performing operations (such asBinarySearch
) that require a List to be sorted. Indexing operations are required to perform in constant access time; that is, O(1).
Some methods, such asContains
,IndexOf
,LastIndexOf
, andRemove
, use an equality comparer for the list elements. The default equality comparer for type T is determined as follows: If type T implements IEquatable<T> then the default equality comparer is System.IEquatable;T>.Equals(T)
; otherwise the default equality comparer is System.Object.Equals(Object)
.Some methods, such as
BinarySearch
andSort
, use a comparer for the list elements. Some overloads of these methods take an explicit comparer as argument, while others use a default comparer. The default comparer for type T is determined as follows: If type T implements System.IComparable<T> then the default comparer is System.IComparable<T>.CompareTo(T)
; otherwise, if type T implements IComparable then the default comparer is System.IComparable.CompareTo(Object)
. If type T implements neither IComparable<T> nor IComparable then there is no default comparer; in this case a comparer or comparison delegate must be given explicitly.The capacity of a List<T> is the number of elements the List<T> can hold. As elements are added to a List<T>, the capacity is automatically increased as required.. The capacity can be decreased by calling System.Collections.Generic.List<T>.TrimToSize or by setting the System.Collections.Generic.List<T>.Capacity property explicitly.
Indexes in this collection are zero-based.
List<T> accepts
null
as a valid value for reference types and allows duplicate elements.This type contains a member that is a nested type, called
Enumerator
. AlthoughEnumerator
is a member of this type,Enumerator
is not described here; instead, it is described in its own entry,List<T>.Enumerator
.
System.Collections.Generic Namespace
List<T> Constructors
List<T>() Constructor
List<T>(System.Collections.Generic.IEnumerable<T>) Constructor
List<T>(int) Constructor
List<T> Methods
List<T>.Add Method
List<T>.AddRange Method
List<T>.AsReadOnly Method
List<T>.BinarySearch(T) Method
List<T>.BinarySearch(T, System.Collections.Generic.IComparer<T>) Method
List<T>.BinarySearch(int, int, T, System.Collections.Generic.IComparer<T>) Method
List<T>.Clear Method
List<T>.Contains Method
List<T>.ConvertAll Method
List<T>.CopyTo(T[]) Method
List<T>.CopyTo(T[], int) Method
List<T>.CopyTo(int, T[], int, int) Method
List<T>.Exists Method
List<T>.Find Method
List<T>.FindAll Method
List<T>.FindIndex(System.Predicate<T>) Method
List<T>.FindIndex(int, System.Predicate<T>) Method
List<T>.FindIndex(int, int, System.Predicate<T>) Method
List<T>.FindLast Method
List<T>.FindLastIndex(System.Predicate<T>) Method
List<T>.FindLastIndex(int, System.Predicate<T>) Method
List<T>.FindLastIndex(int, int, System.Predicate<T>) Method
List<T>.ForEach Method
List<T>.GetEnumerator Method
List<T>.GetRange Method
List<T>.IndexOf(T) Method
List<T>.IndexOf(T, int) Method
List<T>.IndexOf(T, int, int) Method
List<T>.Insert Method
List<T>.InsertRange Method
List<T>.LastIndexOf(T) Method
List<T>.LastIndexOf(T, int) Method
List<T>.LastIndexOf(T, int, int) Method
List<T>.Remove Method
List<T>.RemoveAll Method
List<T>.RemoveAt Method
List<T>.RemoveRange Method
List<T>.Reverse() Method
List<T>.Reverse(int, int) Method
List<T>.Sort() Method
List<T>.Sort(System.Collections.Generic.IComparer<T>) Method
List<T>.Sort(int, int, System.Collections.Generic.IComparer<T>) Method
List<T>.Sort(System.Comparison<T>) Method
List<T>.System.Collections.Generic.IEnumerable<T>.GetEnumerator Method
List<T>.System.Collections.ICollection.CopyTo Method
List<T>.System.Collections.IEnumerable.GetEnumerator Method
List<T>.System.Collections.IList.Add Method
List<T>.System.Collections.IList.Contains Method
List<T>.System.Collections.IList.IndexOf Method
List<T>.System.Collections.IList.Insert Method
List<T>.System.Collections.IList.Remove Method
List<T>.System.Collections.IList.RemoveAt Method
List<T>.ToArray Method
List<T>.TrimExcess Method
List<T>.TrimToSize Method
List<T>.TrueForAll Method
List<T> Properties
List<T>.Capacity Property
List<T>.Count Property
List<T>.Item Property
List<T>.System.Collections.Generic.ICollection<T>.IsReadOnly Property
List<T>.System.Collections.ICollection.IsSynchronized Property
List<T>.System.Collections.ICollection.SyncRoot Property
List<T>.System.Collections.IList.IsFixedSize Property
List<T>.System.Collections.IList.IsReadOnly Property
List<T>.System.Collections.IList.Item Property
public List()
Initializes a new list that is empty and has the default initial capacity.
[Note: If the size of the collection can be estimated, you can specify the initial capacity in a constructor overload that accepts a capacity parameter to eliminate the need to perform a number of resizing operations while adding elements to the list.]
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public List(IEnumerable<T> collection)
Initializes a new list with elements copied from the specified collection, ensuring that the list has sufficient capacity to accommodate the number of elements copied.
- collection
- The collection from which to copy the elements.
Exception Type Condition ArgumentNullException collection is null
.
[Note: If the size of the collection can be estimated, you can specify the initial capacity in a constructor overload that accepts a capacity parameter to eliminate the need to perform a number of resizing operations while adding elements to the list.]
The elements are copied onto the list in the same order in which they are read by the System.Collections.Generic.IEnumerator<T> from collection.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public List(int capacity)
Initializes a new list that is empty and has the specified initial capacity.
- capacity
- The maximum number of elements that the List can contain without reallocating memory.
Exception Type Condition ArgumentOutOfRangeException capacity is less than zero.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public void Add(T item)
Adds an item to the end of the list.
- item
- The item to add to the end of the list. (item can be
null
if T is a reference type.)
List<T> accepts null as a valid value for reference types and allows duplicate elements.If System.Collections.Generic.List<T>.Count already equals System.Collections.Generic.List<T>.Capacity, the capacity of the list is increased.
If System.Collections.Generic.List<T>.Count is less than System.Collections.Generic.List<T>.Capacity, this method is an O(1) operation. If the capacity needs to be increased to accommodate the new element, this method becomes an O(n) operation, where n is System.Collections.Generic.List<T>.Count.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public void AddRange(IEnumerable<T> collection)
Adds the elements of the specified collection to the end of the list.
- collection
- The collection whose elements are added to the end of the list.
Exception Type Condition ArgumentNullException collection is null
.
List<T> acceptsnull
as a valid value for reference types and allows duplicate elements.The order of the elements in the collection is preserved in the List<T>.
If the new System.Collections.Generic.List<T>.Count (the current System.Collections.Generic.List<T>.Count plus the size of the collection) will be greater than System.Collections.Generic.List<T>.Capacity, the capacity of the list is increased.
If the list can accommodate the new elements without increasing System.Collections.Generic.List<T>.Capacity, this method is an O(n) operation, where n is the number of elements to be added. If the capacity needs to be increased to accommodate the new elements, this method becomes an O(n + m) operation, where n is the number of elements to be added and m is System.Collections.Generic.List<T>.Count.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public IList<T> AsReadOnly()
Returns a read-only wrapper to the current List.
A read-only wrapper for the current List.
To prevent any modifications to a list, expose it only through this wrapper.A collection that is read-only is simply a collection with a wrapper that prevents modifying the collection; therefore, if changes are made to the underlying collection, the read-only collection reflects those changes.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public int BinarySearch(T item)
Searches the entire sorted list for an element using the default comparer, and returns the zero-based index of the element.
- item
- The element for which to search. (item can be
null
if T is a reference type.)
The zero-based index of item in the sorted list, if item is found; otherwise, a negative number, which is the bitwise complement of the index of the next element that is larger than item or, if there is no larger element, the bitwise complement of System.Collections.Generic.List<T>.Count.
Exception Type Condition InvalidOperationException The default comparer cannot find a IComparable<T> or IComparable implementation for type T.
This method uses the default comparer for type T to determine the order of list elements. If there is no default comparer, then the method throws InvalidOperationException. The default comparer for a given element type T is defined in the Description section of this (classList<T>
) specification.The list must already be sorted according to the comparer implementation; otherwise, the result is incorrect.
Comparing
null
with any reference type is allowed and does not generate an exception when using IComparable<T>. When sorting,null
is considered to be less than any other object.If the list contains more than one element with the same value, the method returns only one of the occurrences, and it might return any one of the occurrences, not necessarily the first one.
If the list does not contain the specified value, the method returns a negative integer. You can apply the bitwise complement operation (~) to this negative integer to get the index of the first element that is larger than the search value. When inserting the value into the list, this index should be used as the insertion point to maintain the sort order.
This method is an O(log n) operation, where n is the number of elements in the list.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public int BinarySearch(T item, IComparer<T> comparer)
Searches the entire sorted list for an element using the specified comparer and returns the zero-based index of the element.
- item
- The element for which to search. (item can be
null
if T is a reference type.)- comparer
- The IComparer<T> implementation to use when comparing elements.
-or-
null
to use the default comparer.
The zero-based index of item in the sorted list, if item is found; otherwise, a negative number, which is the bitwise complement of the index of the next element that is larger than item or, if there is no larger element, the bitwise complement of System.Collections.Generic.List<T>.Count.
Exception Type Condition InvalidOperationException comparer is null
, and the default comparer cannot find a IComparable<T> orIComparable implementation for type T.
If the given comparer is non-null
, it is used to determine the order of list elements. If the given comparer isnull
, the default comparer for type T is used; if there is no default comparer, then the method throws InvalidOperationException. The default comparer for a given element type T is defined in the Description section of this (classList<T>
) specification.The comparer customizes how the elements are compared. For example, if T is String, you can use a System.Collections.CaseInsensitiveComparer instance as the comparer to perform case-insensitive string searches.
The list must already be sorted according to the comparer implementation; otherwise, the result is incorrect.
Comparing
null
with any reference type is allowed and does not generate an exception when using IComparable<T>. When sorting,null
is considered to be less than any other object.If the List<T> contains more than one element with the same value, the method returns only one of the occurrences, and it might return any one of the occurrences, not necessarily the first one.
If the list does not contain the specified value, the method returns a negative integer. You can apply the bitwise complement operation (~) to this negative integer to get the index of the first element that is larger than the search value. When inserting the value into the list, this index should be used as the insertion point to maintain the sort order.
This method is an O(log n) operation, where n is the number of elements in the list.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public int BinarySearch(int index, int count, T item, IComparer<T> comparer)
Searches a range of elements in the sorted list for an element using the specified comparer and returns the zero-based index of the element.
- index
- The zero-based starting index of the range to search.
- count
- The length of the range to search.
- item
- The element for which to search. (item can be
null
if T is a reference type.)- comparer
- The IComparer<T> implementation to use when comparing elements.
-or-
null
to use the default comparer.
The zero-based index of item in the sorted list, if item is found; otherwise, a negative number, which is the bitwise complement of the index of the next element that is larger than item or, if there is no larger element, the bitwise complement of index + count.
Exception Type Condition ArgumentException index + count is greater than System.Collections.Generic.List<T>.Count. ArgumentOutOfRangeException index is less than zero. -or-
count is less than zero.
InvalidOperationException comparer is null
, and the default comparer cannot find a IComparable<T> orIComparable implementation for type T.
If the given comparer is non-null
, it is used to determine the order of list elements. If the given comparer isnull
, the default comparer for type T is used; if there is no default comparer, then the method throws InvalidOperationException. The default comparer for a given element type T is defined in the Description section of this (classList<T>
) specification.The comparer customizes how the elements are compared. For example, if T is String, you can use a System.Collections.CaseInsensitiveComparer instance as the comparer to perform case-insensitive string searches.
The list must already be sorted according to the comparer implementation; otherwise, the result is incorrect.
Comparing
null
with any reference type is allowed and does not generate an exception when using IComparable<T>. When sorting,null
is considered to be less than any other object.If the List<T> contains more than one element with the same value, the method returns only one of the occurrences, and it might return any one of the occurrences, not necessarily the first one.
If the list does not contain the specified value, the method returns a negative integer. You can apply the bitwise complement operation (~) to this negative integer to get the index of the first element that is larger than the search value. When inserting the value into the list, this index should be used as the insertion point to maintain the sort order.
This method is an O(log n) operation, where n is the number of elements in the range.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public void Clear()
Removes all elements from the list.
System.Collections.Generic.ICollection<T>.Count gets set to zero, and references to other objects from elements of the collection are also released. The capacity remains unchanged.[Note: To reset the capacity, call System.Collections.Generic.List<T>.TrimToSize or set the System.Collections.Generic.List<T>.Capacity property directly.
]
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public bool Contains(T item)
Determines whether the list contains a specific value.
- item
- The object to locate in the current collection. (item can be
null
if T is a reference type.)
true
, if item is found in the list; otherwise,false
.
This method uses the default equality comparer for type T to determine equality of list elements. The default equality comparer for element type T is defined in the Description section of this (classList<T>
) specification.This method is an O(n) operation, where n is System.Collections.Generic.List<T>.Count.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public List<U> ConvertAll<U>(Converter<T,U> converter)
Converts the current List (of type T) to a List of type U.
- converter
- A converter delegate that converts each element from one type to another type.
A List of the target type containing the converted elements from the current List.
Exception Type Condition ArgumentNullException converter is null
.
The converter is a delegate that converts an object to the target type. The elements of the current List are individually passed to the converter delegate, and the converted elements are saved in the new List.The current List remains unchanged.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public void CopyTo(T[] array)
Copies the entire list to an array.
- array
- A one-dimensional, zero-based array that is the destination of the elements copied from the list.
Exception Type Condition ArgumentException array is multidimensional. -or-
array does not have zero-based indexing.
-or-
The number of elements in the list is greater than the number of elements that the destination array can contain.
-or-
Type T is not assignable to the element type of the destination array.
ArgumentNullException array is null
.
The elements are copied onto the array (using System.Array.Copy) in the same order in which the enumerator iterates through the list.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public void CopyTo(T[] array, int arrayIndex)
Copies the elements of the list to an array, starting at a particular index.
- array
- A one-dimensional, zero-based array that is the destination of the elements copied from the list.
- arrayIndex
- The zero-based index in array at which copying begins.
Exception Type Condition ArgumentException array is multidimensional. -or-
array does not have zero-based indexing.
-or-
The sum of arrayIndex and number of elements in the list is greater than the length of the destination array.
-or-
Type T is not assignable to the element type of the destination array.
ArgumentNullException array is null
.
ArgumentOutOfRangeException arrayIndex is less than zero.
The elements are copied onto the array (using System.Array.Copy) in the same order in which the enumerator iterates through the list.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public void CopyTo(int index, T[] array, int arrayIndex, int count)
Copies a range of elements of the list to an array, starting at a particular index in the target array.
- index
- The zero-based index in the source list at which copying begins.
- array
- A one-dimensional, zero-based array that is the destination of the elements copied from the list.
- arrayIndex
- The zero-based index in array at which copying begins.
- count
- The number of elements to copy.
Exception Type Condition ArgumentException array is multidimensional. -or-
index is equal to or greater than the System.Collections.Generic.List<T>.Count of the source list.
-or-
arrayIndex is equal to or greater than the length of array.
-or-
The number of elements from indexto the end of the source list is greater than the available space from arrayIndex to the end of the destination array.
-or-
Type T is not assignable to the element type of the destination array.
ArgumentNullException array is null
.
ArgumentOutOfRangeException index is less than zero. -or-
array does not have zero-based indexing.
-or-
arrayIndex is less than zero.
-or-
count is less than zero.
The elements are copied onto the array (using System.Array.Copy) in the same order in which the enumerator iterates through the list.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public bool Exists(Predicate<T> match)
Determines whether the List contains elements that match the conditions defined by the specified predicate.
- match
- The predicate delegate that specifies the elements to search for.
true
if the List contains one or more elements that match the conditions defined by the specified predicate; otherwise,false
.
Exception Type Condition ArgumentNullException match is null
.
The predicate is a delegate that returnstrue
if the object passed to it matches the conditions defined in the delegate. The elements of the current List are individually passed to the predicate delegate, and processing is stopped when a match is found.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public T Find(Predicate<T> match)
Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire List.
- match
- The predicate delegate that specifies the element to search for.
The first element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type T.
Exception Type Condition ArgumentNullException match is null
.
The predicate is a delegate that returnstrue
if the object passed to it matches the conditions defined in the delegate. The elements of the current List are individually passed to the predicate delegate, moving forward in the List, starting with the first element and ending with the last element. Processing is stopped when a match is found.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public List<T> FindAll(Predicate<T> match)
Retrieves all the elements that match the conditions defined by the specified predicate.
- match
- The predicate delegate that specifies the elements to search for.
A List containing all the elements that match the conditions defined by the specified predicate, if found; otherwise, an empty List.
Exception Type Condition ArgumentNullException match is null
.
The predicate is a delegate that returnstrue
if the object passed to it matches the conditions defined in the delegate. The elements of the current List are individually passed to the Predicate delegate, and the elements that match the conditions are saved in the returned List.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public int FindIndex(Predicate<T> match)
Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the List.
- match
- The predicate delegate that specifies the element to search for.
The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.
Exception Type Condition ArgumentNullException match is null
.
The List is searched forward starting at the first element and ending at the last element.The predicate is a delegate that returns
true
if the object passed to it matches the conditions defined in the delegate. The elements of the current List are individually passed to the predicate delegate.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public int FindIndex(int index, Predicate<T> match)
Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the range of elements in the List that extends from the specified index to the last element.
- index
- The zero-based starting index of the search.
- match
- The predicate delegate that specifies the element to search for.
The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.
Exception Type Condition ArgumentNullException match is null
.
ArgumentOutOfRangeException index is less than 0 or greater than or equal to System.Collections.Generic.List<T>.Count.
The List is searched forward starting at index and ending at the last element.The predicate is a delegate that returns
true
if the object passed to it matches the conditions defined in the delegate. The elements of the current List are individually passed to the predicate delegate.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public int FindIndex(int index, int count, Predicate<T> match)
Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the range of elements in the List that starts at the specified index and contains the specified number of elements.
- index
- The zero-based starting index of the search.
- count
- The number of elements to search.
- match
- The predicate delegate that specifies the element to search for.
The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.
Exception Type Condition ArgumentNullException match is null
.
ArgumentOutOfRangeException index is less than 0. -or-
count is less than 0.
-or-
index + count is greater than System.Collections.Generic.List<T>.Count.
The List is searched forward starting at index and ending after count elements.The predicate is a delegate that returns
true
if the object passed to it matches the conditions defined in the delegate. The elements of the current List are individually passed to the predicate delegate.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public T FindLast(Predicate<T> match)
Searches for an element that matches the conditions defined by the specified predicate, and returns the last occurrence within the entire List.
- match
- The predicate delegate that specifies the element to search for.
The last element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type T.
Exception Type Condition ArgumentNullException match is null
.
The predicate is a delegate that returnstrue
if the object passed to it matches the conditions defined in the delegate. The elements of the current List are individually passed to the predicate delegate, moving backward in the List, starting with the last element and ending with the first element. Processing is stopped when a match is found.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public int FindLastIndex(Predicate<T> match)
Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the last occurrence within the List.
- match
- The predicate delegate that specifies the element to search for.
The zero-based index of the last occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.
Exception Type Condition ArgumentNullException match is null
.
The List is searched backward starting at the last element and ending at the first element.The predicate is a delegate that returns
true
if the object passed to it matches the conditions defined in the delegate. The elements of the current List are individually passed to the predicate delegate.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public int FindLastIndex(int index, Predicate<T> match)
Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the last occurrence within the range of elements in the List that extends from the specified index to the first element.
- index
- The zero-based starting index of the backward search.
- match
- The predicate delegate that specifies the element to search for.
The zero-based index of the last occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.
Exception Type Condition ArgumentNullException match is null
.
ArgumentOutOfRangeException index is less than 0 or greater than or equal to System.Collections.Generic.List<T>.Count.
The List is searched backward starting at index and ending at the first element.The predicate is a delegate that returns
true
if the object passed to it matches the conditions defined in the delegate. The elements of the current List are individually passed to the predicate delegate.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public int FindLastIndex(int index, int count, Predicate<T> match)
Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the last occurrence within the range of elements in the List that starts at the specified index and contains the specified number of elements going backwards.
- index
- The zero-based starting index of the search.
- count
- The number of elements to search.
- match
- The predicate delegate that specifies the element to search for.
The zero-based index of the last occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.
Exception Type Condition ArgumentNullException match is null
.
ArgumentOutOfRangeException index is less than zero, or greater than or equal to System.Collections.Generic.List<T>.Count. -or-
count is less than 0.
-or-
count is greater than index + 1.
The List is searched backward starting at index and ending after count elements.The predicate is a delegate that returns
true
if the object passed to it matches the conditions defined in the delegate. The elements of the current List are individually passed to the predicate delegate.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public void ForEach(Action<T> action)
Performs the specified action on each element of the List.
- action
- The action delegate to perform on each element of the List.
Exception Type Condition ArgumentNullException action is null
.
The action is a delegate that performs an action on the object passed to it. The elements of the current List are individually passed to the action delegate, sequentially, in index order, and on the same thread as that used to callForEach
. Execution stops if the action throws an exception.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public List<T>.Enumerator GetEnumerator()
Returns an enumerator, in index order, that can be used to iterate over the list.
An enumerator for the list.
[Usage: For a detailed description regarding the use of an enumerator, see IEnumerator<T>.]
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public List<T> GetRange(int index, int count)
Creates a shallow copy of a range of elements in the current List.
- index
- The zero-based index at which the range starts.
- count
- The number of elements in the range.
A shallow copy of the given range of elements in the list.
Exception Type Condition ArgumentException index + count is greater than System.Collections.Generic.List<T>.Count.
ArgumentOutOfRangeException index is less than 0. -or-
count is less than 0.
A shallow copy of a collection, or a subset of that collection, copies only the elements of the collection, whether they are reference types or value types, but it does not copy the objects that the references refer to. The references in the new collection point to the same objects as do the references in the original collection. (In contrast, a deep copy of a collection copies the elements and everything directly or indirectly referenced by those elements.)
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public int IndexOf(T value)
Searches for the specified object and returns the zero-based index of the first occurrence within the entire list.
- value
- The T to locate in the current list. (The value can be
null
if T is a reference type.)
The zero-based index of the first occurrence of item within the List, if found; otherwise, -1.
The list is searched forward starting at the first element and ending at the last element.This method uses the default equality comparer for type T to determine equality of list elements. The default equality comparer for element type T is defined in the Description section of this (class List<T>) specification.
This method performs a linear search; therefore, the average number of comparisons is proportional to System.Collections.Generic.List<T>.Count. That is, this method is an O(n) operation, where n is System.Collections.Generic.List<T>.Count.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public int IndexOf(T value, int index)
Searches for the specified object and returns the zero-based index of the first occurrence within the range of elements in the list that extends from the specified index to the last element.
- value
- The T to locate in the current list. (The value can be
null
if T is a reference type.)- index
- The zero-based starting index of the search.
The zero-based index of the first occurrence of item within the range of elements in the list, if found; otherwise, -1.
Exception Type Condition ArgumentOutOfRangeException index is less than zero or greater than System.Collections.Generic.List<T>.Count.
The list is searched forward starting at index and ending at the last element.This method uses the default equality comparer for type T to determine equality of list elements. The default equality comparer for element type T is defined in the Description section of this (class List<T>) specification.
This method is an O(n) operation, where n is the number of elements from index to the end of the list.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public int IndexOf(T value, int index, int count)
Searches for the specified object and returns the zero-based index of the first occurrence within the range of elements in the list that starts at the specified index and contains the specified number of elements.
- value
- The T to locate in the current list. (The value can be
null
if T is a reference type.)- index
- The zero-based starting index of the search.
- count
- The number of elements to search.
The zero-based index of the first occurrence of item within the specified range of elements in the list, if found; otherwise, -1.
Exception Type Condition ArgumentOutOfRangeException index is less than 0. -or-
count is less than 0.
-or-
index + count is greater than System.Collections.Generic.List<T>.Count.
The list is searched forward starting at index and ending at index + count - 1, and searching at most count terms.This method uses the default equality comparer for type T to determine equality of list elements. The default equality comparer for element type T is defined in the Description section of this (class List<T>) specification.
This method is an O(n) operation, where n is count.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public void Insert(int index, T item)
Inserts an item to the List at the specified position.
- index
- The zero-based index at which item is to be inserted.
- item
- The item to insert. (item can be
null
if T is a reference type.)
The zero-based index of the first occurrence of item within the specified range of elements in the list, if found; otherwise, -1.
Exception Type Condition ArgumentOutOfRangeException index is less than 0. -or-
index is greater than System.Collections.Generic.List<T>.Count.
List<T> acceptsnull
as a valid value for reference types and allows duplicate elements.If System.Collections.Generic.List<T>.Count already equals System.Collections.Generic.List<T>.Capacity, the capacity of the List is increased.
If index is equal to System.Collections.Generic.List<T>.Count, item is added to the end of list.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public void InsertRange(int index, IEnumerable<T> collection)
Inserts the elements of a collection in the List at the specified position.
- index
- The zero-based index at which the new elements should be inserted.
- collection
- The collection whose elements should be inserted into the list. (collection itself cannot be
null
, but the collection can contain elements that arenull
, if type T is a reference type.)
The zero-based index of the first occurrence of item within the specified range of elements in the list, if found; otherwise, -1.
Exception Type Condition ArgumentNullException collection is null
.
ArgumentOutOfRangeException index is less than zero, -or-
index is greater than System.Collections.Generic.List<T>.Count.
List<T> acceptsnull
as a valid value for reference types and allows duplicate elements.If the new value of System.Collections.Generic.List<T>.Count will be greater than System.Collections.Generic.List<T>.Capacity, the capacity of the List is increased.
If index is equal to System.Collections.Generic.List<T>.Count, the collection is added to the end of list.
The order of the elements in the collection is preserved in the list.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public int LastIndexOf(T value)
Searches for the specified object and returns the zero-based index of the last occurrence within the entire list.
- value
- The T to locate in the current list. (The value can be
null
if T is a reference type.)
The zero-based index of the last occurrence of item within the entire list, if found; otherwise, -1.
The list is searched backward starting at the last element and ending at the first element.This method uses the default equality comparer for type T to determine equality of list elements. The default equality comparer for element type T is defined in the Description section of this (class List<T>) specification.
This method is an O(n) operation, where n is System.Collections.Generic.List<T>.Count.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public int LastIndexOf(T value, int index)
Searches for the specified object and returns the zero-based index of the last occurrence within the range of elements in the list that extends from the specified index to the last element.
- value
- The T to locate in the current list. (The value can be
null
if T is a reference type.)- index
- The zero-based starting index of the search.
The zero-based index of the last occurrence of item within the range of elements in the list, if found; otherwise, -1.
Exception Type Condition ArgumentOutOfRangeException index is less than zero or greater than or equal to System.Collections.Generic.List<T>.Count.
The list is searched backward starting at index and ending at the first element.This method uses the default equality comparer for type T to determine equality of list elements. The default equality comparer for element type T is defined in the Description section of this (class List<T>) specification.
This method is an O(n) operation, where n is the number of elements from the beginning of the list to index.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public int LastIndexOf(T value, int index, int count)
Searches for the specified object and returns the zero-based index of the last occurrence within the range of elements in the list that starts at the specified index and contains the specified number of elements.
- value
- The T to locate in the current list. (The value can be
null
if T is a reference type.)- index
- The zero-based starting index of the search.
- count
- The number of elements to search.
The zero-based index of the last occurrence of item within the range of elements in the list that contains count number of elements and ends at index, if found; otherwise, -1.
Exception Type Condition ArgumentOutOfRangeException index is less than zero, or greater than or equal to System.Collections.Generic.List<T>.Count. -or-
count is less than 0.
-or-
count is greater than index + 1.
The list is searched backward starting at index and ending after count elements.This method uses the default equality comparer for type T to determine equality of list elements. The default equality comparer for element type T is defined in the Description section of this (class List<T>) specification.
This method is an O(n) operation, where n is count.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public bool Remove(T item)
Removes the first occurrence of the specified object from the list.
- item
- The object to be removed from the list.
true
if item is successfully removed; otherwise,false
.
This method uses the default equality comparer for type T to determine equality of list elements. The default equality comparer for element type T is defined in the Description section of this (class List<T>) specification.This method is an O(n) operation, where n is System.Collections.Generic.List<T>.Count.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public int RemoveAll(Predicate<T> match)
Removes the all the elements that match the conditions defined by the specified predicate.
- match
- The predicate delegate that specifies the elements to remove.
The number of elements removed from the List.
Exception Type Condition ArgumentNullException match is null
.
The predicate is a delegate that returnstrue
if the object passed to it matches the conditions defined in the delegate. The elements of the current List are individually passed to the predicate delegate, and the elements that match the conditions are removed from the List.This method is an O(n) operation, where n is System.Collections.Generic.List<T>.Count.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public void RemoveAt(int index)
Removes the item at the specified index of the list.
- index
- The zero-based index of the item to remove.
Exception Type Condition ArgumentOutOfRangeException index is less than 0. -or-
index is equal to or greater than System.Collections.Generic.List<T>.Count.
The item is removed and all the elements following it in the List have their indexes reduced by 1.This method is an O(n) operation, where n is System.Collections.Generic.List<T>.Count.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public void RemoveRange(int index, int count)
Removes a range of elements from the list.
- index
- The zero-based starting index of the range of elements to remove.
- count
- The number of elements to remove.
Exception Type Condition ArgumentException index + count is greater than System.Collections.Generic.List<T>.Count.
ArgumentOutOfRangeException index is less than zero. -or-
count is less than zero.
The items are removed and all the elements following them in the List have their indexes reduced by count.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public void Reverse()
Reverses the order of the elements in the list.
This method uses System.Array.Reverse(System.Array)
to reverse the order of the elements.This method is an O(n) operation, where n is System.Collections.Generic.List<T>.Count.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public void Reverse(int index, int count)
Reverses the order of the elements in the specified element range of the list.
- index
- The zero-based starting index of the range of elements to reverse.
- count
- The number of elements to reverse.
Exception Type Condition ArgumentException index + count is greater than System.Collections.Generic.List<T>.Count.
ArgumentOutOfRangeException index is less than zero. -or-
count is less than zero.
This method reverses the order of the elements in the specified element rangeThis method is an O(n) operation, where n is count.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public void Sort()
Sorts the elements in the list using the default comparer.
Exception Type Condition InvalidOperationException The default comparer cannot find a IComparable<T> or IComparable implementation for type T.
This method uses the default comparer for type T to determine the order of list elements. If there is no default comparer, then the method throws InvalidOperationException. The default comparer for a given element type T is defined in the Description section of this (classList<T>
) specification.At worst, this operation is O(n2), where n is the number of elements to sort. On average it's O(n log n).
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public void Sort(IComparer<T> comparer)
Sorts the elements in the list using the specified comparer.
- comparer
- The IComparer<T> implementation to use when comparing elements.
-or-
null
to use the default comparer.
Exception Type Condition InvalidOperationException comparer is null
, and the default comparer cannot find a IComparable<T> or IComparable implementation for type T.
If the given comparer is non-null
, it is used to determine the order of list elements. If the given comparer isnull
, the default comparer for type T is used; if there is no default comparer, then the method throws InvalidOperationException. The default comparer for a given element type T is defined in the Description section of this (classList<T>
) specification.At worst, this operation is O(n2), where n is the number of elements to sort. On average it's O(n log n).
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public void Sort(int index, int count, IComparer<T> comparer)
Sorts the elements in the list using the specified comparer.
- index
- The zero-based starting index of the range of elements to sort.
- count
- The number of elements to sort.
- comparer
- The IComparer<T> implementation to use when comparing elements.
-or-
null
to use the default comparer.
Exception Type Condition ArgumentException index + count is greater than System.Collections.Generic.List<T>.Count. ArgumentOutOfRangeException index is less than zero. -or-
count is less than zero.
InvalidOperationException comparer is null
, and the default comparer cannot find a IComparable<T> or IComparable implementation for type T.
If the given comparer is non-null
, it is used to determine the order of list elements. If the given comparer isnull
, the default comparer for type T is used; if there is no default comparer, then the method throws InvalidOperationException. The default comparer for a given element type T is defined in the Description section of this (classList<T>
) specification.At worst, this operation is O(n2), where n is the number of elements to sort. On average it's O(n log n).
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public void Sort(Comparison<T> comparison)
Sorts the elements in the list using the specified comparison.
- comparison
- The comparison to use when comparing elements.
Exception Type Condition ArgumentNullException comparison is null
.
At worst, this operation is O(n2), where n is the number of elements to sort. On average it's O(n log n).
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
IEnumerator<T> IEnumerable<T>.GetEnumerator()
This method is implemented to support the IEnumerable<T> interface.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
void ICollection.CopyTo(Array array, int index)
This method is implemented to support the ICollection interface.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
IEnumerator IEnumerable.GetEnumerator()
This method is implemented to support the IEnumerable interface.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
int IList.Add(object value)
This method is implemented to support the IList interface.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
bool IList.Contains(object value)
This method is implemented to support the IList interface.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
int IList.IndexOf(object value)
This method is implemented to support the IList interface.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
void IList.Insert(int index, object value)
This method is implemented to support the IList interface.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
void IList.Remove(object value)
This method is implemented to support the IList interface.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
void IList.RemoveAt(int index)
This method is implemented to support the IList interface.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public T[] ToArray()
Copies the elements in the list to a new array.
The new array containing a copy of the list's elements.
This an O(n) operation, where n is System.Collections.Generic.List<T>.Count.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public void TrimExcess()
Suggests that the capacity be reduced to the actual number of elements in the list.
This method can be used to suggest a collection's memory overhead be minimized, e.g., if no new elements are expected to be added to the collection.[Note: To reset a list to its initial state, call the System.Collections.Generic.List.Clear method before calling System.Collections.Generic.List.TrimExcess.]
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public void TrimToSize()
Sets the capacity to the actual number of elements in the list.
This method can be used to minimize a list's memory overhead if no new elements are expected to be added to the list.To reset a List to its initial state, call the System.Collections.Generic.List<T>.Clear method before calling System.Collections.Generic.List<T>.TrimToSize. Trimming an empty list sets the capacity of the list to the default capacity.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public bool TrueForAll(Predicate<T> match)
Determines whether every element in the List matches the conditions defined by the specified predicate.
- match
- The predicate delegate that specifies the check against the elements.
true
, if every element in the List matches the conditions defined by the specified predicate; otherwise,false
.
Exception Type Condition ArgumentNullException match is null
.
The predicate is a delegate that returnstrue
if the object passed to it matches the conditions defined in the delegate. The elements of the current List are individually passed to the predicate delegate. The elements are processed sequentially and on the same thread.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public int Capacity { get; set; }
Gets or sets the number of elements the current instance can contain.
A Int32 containing the number of elements the current instance can contain.
Exception Type Condition ArgumentOutOfRangeException Attempt to set the capacity to a value less than System.Collections.Generic.List<T>.Count.
This property is read/write.System.Collections.Generic.List<T>.Capacity is the number of elements that the list is capable of storing without needing to be extended. System.Collections.Generic.List<T>.Count is the number of elements that are actually in the list.
System.Collections.Generic.List<T>.Capacity is always greater than or equal to System.Collections.Generic.List<T>.Count. When System.Collections.Generic.List<T>.Count exceeds System.Collections.Generic.List<T>.Capacity while adding elements, the capacity is increased.
The capacity can be decreased by calling System.Collections.Generic.List<T>.TrimToSize or by setting the System.Collections.Generic.List<T>.Capacity property explicitly.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public int Count { get; }
Gets the number of elements contained in the current instance.
The number of elements in the current instance.
This property is read-only.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
public T this[int index] { get; set; }
Gets or sets the element at the specified index of the current instance.
- index
- The zero-based index of the element in the current instance to get or set.
The element at the specified index of the current instance.
Exception Type Condition ArgumentOutOfRangeException index < 0. -or-
index >= System.Collections.Generics.List<T>.Count of the current instance.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
bool ICollection<T>.IsReadOnly { get; }
This read-only property is implemented to support the ICollection<T> interface.[Note: For more information, see System.Collections.Generic.ICollection<T>.IsReadOnly.
]
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
bool ICollection.IsSynchronized { get; }
This read-only property is implemented to support the System.Collections.ICollection interface.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
object ICollection.SyncRoot { get; }
This read-only property is implemented to support the System.Collections.ICollection interface.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
bool IList.IsFixedSize { get; }
This read-only property is implemented to support the IList interface.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
bool IList.IsReadOnly { get; }
This read-only property is implemented to support the IList interface.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace
object IList.this[int index] { get; set; }
This read-only property is implemented to support the IList interface.
System.Collections.Generic.List<T> Class, System.Collections.Generic Namespace