Copy List Items Using VB.NET
This article explains how to copy a list in one dimensional array using vb.net.
Copy a List
The CopyTo method copies a List into a one-dimensional array. The CopyTo method has three overloaded forms.
The following code snippet copies the entire list into an Array.
Imports System.Text
Imports System.IO
Imports System.Collections.Generic
Module Module1
Sub Main()
Dim DepartmentList As New List(Of String)()
DepartmentList.Add("Service")
DepartmentList.Add("Finance")
DepartmentList.Add("Maintenance")
DepartmentList.Add("Testing")
DepartmentList.Add("HR")
DepartmentList.Add("Service")
DepartmentList.Add("Finance")
DepartmentList.Add("Maintenance")
DepartmentList.Add("Testing")
DepartmentList.Add("HR")
'Create an array of strings
Dim departmentArray As String() = New String(14) {}
'Copy entire List
DepartmentList.CopyTo(departmentArray)
'Copy items starting at index=4
DepartmentList.CopyTo(departmentArray, 4)
'Copy 4 items starting at index 2 in List and copying
'to array starting at index 10
DepartmentList.CopyTo(2, departmentArray, 3, 4)
For Each department As String In departmentArray
Console.WriteLine(department)
Next
Console.ReadLine()
End Sub
End Module
Output:
