List<T>.RemoveAt (Int32) Method is used to remove the element at the specified index of the List<T>.
Properties of List:
- It is different from the arrays. A list can be resized dynamically but arrays cannot.
- List class can accept null as a valid value for reference types and it also allows duplicate elements.
- If the Count becomes equals to Capacity then the capacity of the List increases automatically by reallocating the internal array. The existing elements will be copied to the new array before the addition of the new element.
Syntax:
public void RemoveAt (int index);
Parameters:
index: It is the zero-based starting index of the element which is to be removed.
count: It is the number of the elements which is to be removed.
Exceptions: This method will give
ArgumentOutOfRangeException if the
index is less than 0 or equal to or greater than
Count.
Below programs illustrate the use of
List<T>.RemoveAt (Int32) Method:
Example 1:
Output:
Elements Present in List:
At Position 0: 17
At Position 1: 19
At Position 2: 21
At Position 3: 9
At Position 4: 75
At Position 5: 19
At Position 6: 73
Removing the element at index 3
At Position 0: 17
At Position 1: 19
At Position 2: 21
At Position 3: 75
At Position 4: 19
At Position 5: 73
Example 2: