Remove Items From List Using VB.NET

This article explains how to remove items from list in VB.NET.
  • 20011

Remove Items

The Remove method removes the first occurrence of a specific object from a List. The Remove method takes an item as its parameter. The following code snippet removes an item from a List.

' Create a list of strings

        Dim AuthorList As New List(Of String)()

        AuthorList.Add("Mahesh Chand")

        AuthorList.Add("Praveen Kumar")

        AuthorList.Add("Raj Kumar")

        AuthorList.Add("Nipun Tomar")

        AuthorList.Add("Dinesh Beniwal")

        AuthorList.Remove("Mahesh Chand")

The RemoveAt method removes an item at the specified zero based index. The following code snippet removes an item at 2nd position in the List.

AuthorList.RemoveAt(2)

The RemoveRange method removes a number of items based on the specified starting index and number of items. The RemoveRange method takes first parameter as the starting position and second parameter as the number of the items to be removed from the List. The following code snippet removes 3 items starting at the 2nd position.

 AuthorList.RemoveRange(2, 3)

The Clear method removes all items from the List. The following code snippet removes all items by calling the Clear method.
The Clear method removes all items from the collection. The following code snippet removes all items by calling the Clear method.

AuthorList.Clear()

For example:

Removes an item at 2nd position in the List.

Imports System.Text

Imports System.IO

Imports System.Collections.Generic

Module Module1

    Sub Main()

      ' Create a list of strings

        Dim AuthorList As New List(Of String)()

        AuthorList.Add("Mahesh Chand")

        AuthorList.Add("Praveen Kumar")

        AuthorList.Add("Raj Kumar")

        AuthorList.Add("Nipun Tomar")

        AuthorList.Add("Dinesh Beniwal")

        AuthorList.Remove("Mahesh Chand")

        For Each Author In AuthorList

            Console.WriteLine(Author)

        Next

        AuthorList.RemoveAt(2)

 

        Console.WriteLine()

        Console.WriteLine()

        Console.WriteLine("List after removal item at 2nd position:")

        For Each Author In AuthorList

            Console.WriteLine(Author)

        Next

        Console.ReadLine()

    End Sub

End Module

Output:

remove-item-from-2nd=position.jpg

Removes 2 items starting at the 3rd position.

Imports System.Text

Imports System.IO

Imports System.Collections.Generic

Module Module1

    Sub Main()

      ' Create a list of strings

        Dim AuthorList As New List(Of String)()

        AuthorList.Add("Mahesh Chand")

        AuthorList.Add("Praveen Kumar")

        AuthorList.Add("Raj Kumar")

        AuthorList.Add("Nipun Tomar")

        AuthorList.Add("Dinesh Beniwal")

        For Each Author In AuthorList

            Console.WriteLine(Author)

        Next

        AuthorList.RemoveRange(2, 3)

        Console.WriteLine()

        Console.WriteLine()

        Console.WriteLine("List after removal of 3 items from 2nd position:")

        For Each Author In AuthorList

            Console.WriteLine(Author)

        Next

        Console.ReadLine()

    End Sub

End Module

Output:

remove-3-items-from-2nd-position.jpg

Clear List:

Imports System.Text

Imports System.IO

Imports System.Collections.Generic

Module Module1

    Sub Main()

      ' Create a list of strings

        Dim AuthorList As New List(Of String)()

        AuthorList.Add("Mahesh Chand")

        AuthorList.Add("Praveen Kumar")

        AuthorList.Add("Raj Kumar")

        AuthorList.Add("Nipun Tomar")

        AuthorList.Add("Dinesh Beniwal")

        For Each Author In AuthorList

            Console.WriteLine(Author)

        Next

        AuthorList.Clear()

        Console.WriteLine()

        Console.WriteLine()

        Console.WriteLine("Clear List ")

        For Each Author In AuthorList

            Console.WriteLine(Author)

        Next

        Console.ReadLine()

    End Sub

End Module


Output:

clear-list.jpg

Categories

More Articles

© 2018 DotNetHeaven. All rights reserved.