Export (0) Print
Expand All
3 out of 8 rated this helpful - Rate this topic

List(Of T).RemoveAt Method

Removes the element at the specified index of the List(Of T).

Namespace:  System.Collections.Generic
Assembly:  mscorlib (in mscorlib.dll)
'Declaration
Public Sub RemoveAt ( _
	index As Integer _
)

Parameters

index
Type: System.Int32

The zero-based index of the element to remove.

Implements

IList(Of T).RemoveAt(Int32)
IList.RemoveAt(Int32)
ExceptionCondition
ArgumentOutOfRangeException

index is less than 0.

-or-

index is equal to or greater than Count.

When you call RemoveAt to remove an item, the remaining items in the list are renumbered to replace the removed item. For example, if you remove the item at index 3, the item at index 4 is moved to the 3 position. In addition, the number of items in the list (as represented by the Count property) is reduced by 1.

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

The following example demonstrates how to add, remove, and insert a simple business object in a List(Of T).

Imports System.Collections.Generic
' Simple business object. A PartId is used to identify the type of part  
' but the part name can change.  
Public Class Part
    Implements IEquatable(Of Part)
    Public Property PartName() As String 
        Get 
            Return m_PartName
        End Get 
        Set(value As String)
            m_PartName = Value
        End Set 
    End Property 
    Private m_PartName As String 

    Public Property PartId() As Integer 
        Get 
            Return m_PartId
        End Get 
        Set(value As Integer)
            m_PartId = Value
        End Set 
    End Property 
    Private m_PartId As Integer 

    Public Overrides Function ToString() As String 
        Return "ID: " & PartId & "   Name: " & PartName
    End Function 
    Public Overrides Function Equals(obj As Object) As Boolean 
        If obj Is Nothing Then 
            Return False 
        End If 
        Dim objAsPart As Part = TryCast(obj, Part)
        If objAsPart Is Nothing Then 
            Return False 
        Else 
            Return Equals(objAsPart)
        End If 
    End Function 
    Public Overrides Function GetHashCode() As Integer 
        Return PartId
    End Function 
    Public Overloads Function Equals(other As Part) As Boolean _
        Implements IEquatable(Of Part).Equals
        If other Is Nothing Then 
            Return False 
        End If 
        Return (Me.PartId.Equals(other.PartId))
    End Function 
    ' Should also override == and != operators. 

End Class 
Public Class Example
    Public Shared Sub Main()
        ' Create a list of parts. 
        Dim parts As New List(Of Part)()

        ' Add parts to the list.
        parts.Add(New Part() With { _
             .PartName = "crank arm", _
             .PartId = 1234 _
        })
        parts.Add(New Part() With { _
             .PartName = "chain ring", _
             .PartId = 1334 _
        })
        parts.Add(New Part() With { _
             .PartName = "regular seat", _
             .PartId = 1434 _
        })
        parts.Add(New Part() With { _
             .PartName = "banana seat", _
             .PartId = 1444 _
        })
        parts.Add(New Part() With { _
             .PartName = "cassette", _
             .PartId = 1534 _
        })
        parts.Add(New Part() With { _
             .PartName = "shift lever", _
             .PartId = 1634 _
        })



        ' Write out the parts in the list. This will call the overridden ToString method 
        ' in the Part class.
        Console.WriteLine()
        For Each aPart As Part In parts
            Console.WriteLine(aPart)
        Next 


        ' Check the list for part #1734. This calls the IEquitable.Equals method 
        ' of the Part class, which checks the PartId for equality.
        Console.WriteLine(vbLf & "Contains(""1734""): {0}", parts.Contains(New Part() With { _
             .PartId = 1734, _
             .PartName = "" _
        }))

        ' Insert a new item at position 2.
        Console.WriteLine(vbLf & "Insert(2, ""1834"")")
        parts.Insert(2, New Part() With { _
             .PartName = "brake lever", _
             .PartId = 1834 _
        })


        'Console.WriteLine(); 
        For Each aPart As Part In parts
            Console.WriteLine(aPart)
        Next

        Console.WriteLine(vbLf & "Parts[3]: {0}", parts(3))

        Console.WriteLine(vbLf & "Remove(""1534"")")

        ' This will remove part 1534 even though the PartName is different, 
        ' because the Equals method only checks PartId for equality.
        parts.Remove(New Part() With { _
             .PartId = 1534, _
             .PartName = "cogs" _
        })

        Console.WriteLine()
        For Each aPart As Part In parts
            Console.WriteLine(aPart)
        Next

        Console.WriteLine(vbLf & "RemoveAt(3)")
        ' This will remove part at index 3.
        parts.RemoveAt(3)

        Console.WriteLine()
        For Each aPart As Part In parts
            Console.WriteLine(aPart)
        Next 
    End Sub 
    
    '        This example code produces the following output: 
    '        ID: 1234   Name: crank arm 
    '        ID: 1334   Name: chain ring 
    '        ID: 1434   Name: regular seat 
    '        ID: 1444   Name: banana seat 
    '        ID: 1534   Name: cassette 
    '        ID: 1634   Name: shift lever 
    
    '        Contains("1734"): False 
    
    '        Insert(2, "1834") 
    '        ID: 1234   Name: crank arm 
    '        ID: 1334   Name: chain ring 
    '        ID: 1834   Name: brake lever 
    '        ID: 1434   Name: regular seat 
    '        ID: 1444   Name: banana seat 
    '        ID: 1534   Name: cassette 
    '        ID: 1634   Name: shift lever 
    
    '        Parts[3]: ID: 1434   Name: regular seat 
    
    '        Remove("1534") 
    
    '        ID: 1234   Name: crank arm 
    '        ID: 1334   Name: chain ring 
    '        ID: 1834   Name: brake lever 
    '        ID: 1434   Name: regular seat 
    '        ID: 1444   Name: banana seat 
    '        ID: 1634   Name: shift lever 
    '   ' 
    '        RemoveAt(3) 
    
    '        ID: 1234   Name: crank arm 
    '        ID: 1334   Name: chain ring 
    '        ID: 1834   Name: brake lever 
    '        ID: 1444   Name: banana seat 
    '        ID: 1634   Name: shift lever 
    '         

End Class

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.