Add Items To List Using VB.NET

In this article I describe how to add items in list.
  • 4665

List Methods

The List class is a generic collection and provides all common methods to add, remove, find and replace items in the collection.

Add Items

The Add method adds an item to a List. The following code snippet creates a List and adds items to it by using the Add method.

' Create a list

Dim AuthorList As New List(Of String)()

' Add items using Add method

AuthorList.Add("Mahesh Chand")

AuthorList.Add("Praveen Kumar")

AuthorList.Add("Raj Kumar")

AuthorList.Add("Nipun Tomar")

AuthorList.Add("Dinesh Beniwal")

 

The AddRange method is used to add a collection of items. The following code snippet adds a collection of items to a List.

 

Dim authors As String() = {"Mike Gold", "Don Box", "Sundar Lal", "Neel Beniwal"}

AuthorList.AddRange(authors)

Here is complete code to add items to list:

Imports System.Text

Imports System.IO

Imports System.Collections.Generic

Module Module1

    Sub Main()

      ' Create a list

        Dim AuthorList As New List(Of String)()

        ' Add items using Add method

        AuthorList.Add("Mahesh Chand")

        AuthorList.Add("Praveen Kumar")

        AuthorList.Add("Raj Kumar")

        AuthorList.Add("Nipun Tomar")

        AuthorList.Add("Dinesh Beniwal")

        Console.WriteLine("AuthorList:")

        For Each author In AuthorList

            Console.WriteLine(author)

        Next

        ' Add a range of items

        Dim authors As String() = {"Mike Gold", "Don Box", "Sundar Lal", "Neel Beniwal"}

        AuthorList.AddRange(authors)

        Console.WriteLine()

        Console.WriteLine()

        Console.WriteLine("AuthorList after adding some more items:")

        For Each Author In AuthorList

            Console.WriteLine(Author)

        Next

        Console.ReadLine()

    End Sub

End Module

Output:

AddItems-to-list.jpg

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.