List.RemoveAt Method (System.Collections.Generic)

Switch View :
ScriptFree
List.RemoveAt Method
Removes the element at the specified index of the List.

Namespace: System.Collections.Generic
Assembly: mscorlib (in mscorlib.dll)

Syntax

Visual Basic (Declaration)
Public Sub RemoveAt ( _
	index As Integer _
)
Visual Basic (Usage)
Dim instance As List(Of T)
Dim index As Integer

instance.RemoveAt(index)
C#
public void RemoveAt (
	int index
)
C++
public:
virtual void RemoveAt (
	int index
) sealed
J#
public final void RemoveAt (
	int index
)
JScript
public final function RemoveAt (
	index : int
)
XAML
Not applicable.

Parameters

index

The zero-based index of the element to remove.

Exceptions

Exception type Condition

ArgumentOutOfRangeException

index is less than 0.

-or-

index is equal to or greater than Count.

Remarks

This method is an O(n) operation, where n is (Count - index).

Platforms

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

Version Information

.NET Framework

Supported in: 3.0, 2.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0
See Also

Community Content

Stanley Roark
Null (Nothing) object remains in list with DataGridView

Don't know if this will be of help to anyone else, but I was updating a List(Of T) from a DataGridView. Due to constraints on the objects, I had it set up to associate the represented object as the tag of each row. Upon closing (and saving) the changes, it would scan the rows, apply the changes to the linked objects, and then create / remove additions or deletions.

I thought the List.Remove() and List.RemoveAt were misbehaving because it kept leaving behind a null value in the list after successful removal of an item. In actuality, I was just not filtering the on the Row.IsNewRow() - which meant after a clean removal, I was adding a null object to my list.


Kristof Verbiest
You cannot remove an item while you're enumerating the list
If you try to remove an item while you are enumerating the list, this will result in an exception. Several ways to work around this are explained here: http://kristofverbiest.blogspot.com/2008/10/removing-items-from-collection.html