Export (0) Print
Expand All

SortedList.CopyTo Method

Copies SortedList elements to a one-dimensional Array object, starting at the specified index in the array.

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

'Declaration
Public Overridable Sub CopyTo ( _
	array As Array, _
	arrayIndex As Integer _
)

Parameters

array
Type: System.Array

The one-dimensional Array object that is the destination of the DictionaryEntry objects copied from SortedList. The Array must have zero-based indexing.

arrayIndex
Type: System.Int32

The zero-based index in array at which copying begins.

Implements

ICollection.CopyTo(Array, Int32)

ExceptionCondition
ArgumentNullException

array is Nothing.

ArgumentOutOfRangeException

arrayIndex is less than zero.

ArgumentException

array is multidimensional.

-or-

The number of elements in the source SortedList object is greater than the available space from arrayIndex to the end of the destination array.

InvalidCastException

The type of the source SortedList cannot be cast automatically to the type of the destination array.

The key/value pairs are copied to the Array object in the same order in which the enumerator iterates through the SortedList object.

To copy only the keys in the SortedList, use SortedList.Keys.CopyTo.

To copy only the values in the SortedList, use SortedList.Values.CopyTo.

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

The following code example shows how to copy the values in a SortedList object into a one-dimensional Array object.

Imports System
Imports System.Collections

Public Class SamplesSortedList

   Public Shared Sub Main()

      ' Creates and initializes the source SortedList. 
      Dim mySourceList As New SortedList()
      mySourceList.Add(2, "cats")
      mySourceList.Add(3, "in")
      mySourceList.Add(1, "napping")
      mySourceList.Add(4, "the")
      mySourceList.Add(0, "three")
      mySourceList.Add(5, "barn")

      ' Creates and initializes the one-dimensional target Array. 
      Dim tempArray() As String = {"The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog"}
      Dim myTargetArray(14) As DictionaryEntry
      Dim i As Integer = 0
      Dim s As String 
      For Each s In  tempArray
         myTargetArray(i).Key = i
         myTargetArray(i).Value = s
         i += 1
      Next s

      ' Displays the values of the target Array.
      Console.WriteLine("The target Array contains the following (before and after copying):")
      PrintValues(myTargetArray, " "c)

      ' Copies the entire source SortedList to the target SortedList, starting at index 6.
      mySourceList.CopyTo(myTargetArray, 6)

      ' Displays the values of the target Array.
      PrintValues(myTargetArray, " "c)

   End Sub 'Main


   Public Shared Sub PrintValues(myArr() As DictionaryEntry, mySeparator As Char)
      Dim i As Integer 
      For i = 0 To myArr.Length - 1
         Console.Write("{0}{1}", mySeparator, myArr(i).Value)
      Next i
      Console.WriteLine()
  End Sub 'PrintValues

End Class 'SamplesSortedList 


'This code produces the following output. 
'  
'The target Array contains the following (before and after copying): 
' The quick brown fox jumped over the lazy dog       
' The quick brown fox jumped over three napping cats in the barn

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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.

Show:
© 2014 Microsoft