Dictionary<TKey, TValue>.ICollection<KeyValuePair<TKey, TValue>>.CopyTo Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Copies the elements of the ICollection<T> to an array of type KeyValuePair<TKey, TValue>, starting at the specified array index.

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

Syntax

'Declaration
Private Sub CopyTo ( _
    array As KeyValuePair(Of TKey, TValue)(), _
    index As Integer _
) Implements ICollection(Of KeyValuePair(Of TKey, TValue)).CopyTo
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(
    KeyValuePair<TKey, TValue>[] array,
    int index
)

Parameters

  • index
    Type: System.Int32
    The zero-based index in array at which copying begins.

Implements

ICollection<T>.CopyTo(array<T[], Int32)

Exceptions

Exception Condition
ArgumentNullException

array is nulla null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

index is less than 0.

ArgumentException

index is equal to or greater than the length of array.

-or-

The number of elements in the source ICollection<T> is greater than the available space from index to the end of the destination array.

Remarks

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

Examples

The following example shows how to use explicit interface implementations of the System.Collections.Generic.ICollection<T> generic interface to manipulate a Dictionary<TKey, TValue> object.

Imports System.Collections.Generic

Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' Create a new dictionary of strings, with string keys, and
      ' access it through the generic ICollection interface. The
      ' generic ICollection interface views the dictionary as a
      ' collection of KeyValuePair objects with the same type
      ' arguments as the dictionary.
      '
      Dim openWith As ICollection(Of KeyValuePair(Of String, String)) _
          = New Dictionary(Of String, String)

      ' Add some elements to the dictionary. When elements are 
      ' added through the ICollection(Of T) interface, the keys
      ' and values must be wrapped in KeyValuePair objects.
      '
      openWith.Add(New KeyValuePair(Of String, String)("txt", "notepad.exe"))
      openWith.Add(New KeyValuePair(Of String, String)("bmp", "paint.exe"))
      openWith.Add(New KeyValuePair(Of String, String)("dib", "paint.exe"))
      openWith.Add(New KeyValuePair(Of String, String)("rtf", "wordpad.exe"))

      outputBlock.Text &= vbCrLf
      For Each element As KeyValuePair(Of String, String) In openWith
         outputBlock.Text &= String.Format("{0}, {1}", element.Key, element.Value) & vbCrLf
      Next

      ' The Contains method also takes a KeyValuePair object.
      '
      outputBlock.Text &= String.Format(vbLf & _
          "Contains(KeyValuePair(""txt"", ""notepad.exe"")): {0}", _
          openWith.Contains(New KeyValuePair(Of String, String)("txt", "notepad.exe"))) & vbCrLf

      ' The Remove method takes a KeyValuePair object.)
      '
      ' Use the Remove method to remove a key/value pair.
      outputBlock.Text &= String.Format(vbLf & _
          "Remove(New KeyValuePair(""dib"", ""paint.exe""))") & vbCrLf
      openWith.Remove(New KeyValuePair(Of String, String)("dib", "paint.exe"))

      outputBlock.Text &= vbCrLf
      For Each element As KeyValuePair(Of String, String) In openWith
         outputBlock.Text &= String.Format("{0}, {1}", element.Key, element.Value) & vbCrLf
      Next

      ' Create an array of KeyValuePair objects and copy the 
      ' contents of the dictionary to it. Subtract one from the
      ' array size because Visual Basic allocates an extra array
      ' element.
      Dim copy(openWith.Count - 1) As KeyValuePair(Of String, String)
      openWith.CopyTo(copy, 0)

      ' List the contents of the array.
      '
      outputBlock.Text &= vbCrLf
      For Each element As KeyValuePair(Of String, String) In copy
         outputBlock.Text &= String.Format("{0}, {1}", element.Key, element.Value) & vbCrLf
      Next

   End Sub

End Class

' This code example produces the following output:
'
'txt, notepad.exe
'bmp, paint.exe
'dib, paint.exe
'rtf, wordpad.exe
'
'Contains(KeyValuePair("txt", "notepad.exe")): True
'
'Remove(New KeyValuePair("dib", "paint.exe"))
'
'txt, notepad.exe
'bmp, paint.exe
'rtf, wordpad.exe
'
'txt, notepad.exe
'bmp, paint.exe
'rtf, wordpad.exe 
using System;
using System.Collections.Generic;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Create a new dictionary of strings, with string keys, and
      // access it through the generic ICollection interface. The
      // generic ICollection interface views the dictionary as a
      // collection of KeyValuePair objects with the same type
      // arguments as the dictionary.
      //
      ICollection<KeyValuePair<String, String>> openWith =
          new Dictionary<String, String>();

      // Add some elements to the dictionary. When elements are 
      // added through the ICollection<T> interface, the keys
      // and values must be wrapped in KeyValuePair objects.
      //
      openWith.Add(new KeyValuePair<String, String>("txt", "notepad.exe"));
      openWith.Add(new KeyValuePair<String, String>("bmp", "paint.exe"));
      openWith.Add(new KeyValuePair<String, String>("dib", "paint.exe"));
      openWith.Add(new KeyValuePair<String, String>("rtf", "wordpad.exe"));

      outputBlock.Text += "\n";
      foreach (KeyValuePair<string, string> element in openWith)
      {
         outputBlock.Text += String.Format("{0}, {1}", element.Key, element.Value) + "\n";
      }

      // The Contains method also takes a KeyValuePair object.
      //
      outputBlock.Text += String.Format(
          "\nContains(KeyValuePair(\"txt\", \"notepad.exe\")): {0}",
          openWith.Contains(new KeyValuePair<String, String>("txt", "notepad.exe"))) + "\n";

      // The Remove method takes a KeyValuePair object.)
      //
      // Use the Remove method to remove a key/value pair.
      outputBlock.Text += String.Format("\nRemove(new KeyValuePair(\"dib\", \"paint.exe\"))") + "\n";
      openWith.Remove(new KeyValuePair<String, String>("dib", "paint.exe"));

      outputBlock.Text += "\n";
      foreach (KeyValuePair<string, string> element in openWith)
      {
         outputBlock.Text += String.Format("{0}, {1}", element.Key, element.Value) + "\n";
      }

      // Create an array of KeyValuePair objects and copy the 
      // contents of the dictionary to it. 
      // 
      KeyValuePair<string, string>[] copy =
          new KeyValuePair<string, string>[openWith.Count];
      openWith.CopyTo(copy, 0);

      // List the contents of the array.
      //
      outputBlock.Text += "\n";
      foreach (KeyValuePair<string, string> element in copy)
      {
         outputBlock.Text += String.Format("{0}, {1}", element.Key, element.Value) + "\n";
      }
   }
}

/* This code example produces the following output:

txt, notepad.exe
bmp, paint.exe
dib, paint.exe
rtf, wordpad.exe

Contains(KeyValuePair("txt", "notepad.exe")): True

Remove(new KeyValuePair("dib", "paint.exe"))

txt, notepad.exe
bmp, paint.exe
rtf, wordpad.exe

txt, notepad.exe
bmp, paint.exe
rtf, wordpad.exe
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.