Export (0) Print
Expand All

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

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)

void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(
	KeyValuePair<TKey, TValue>[] array,
	int index
)

Parameters

array
Type: System.Collections.Generic.KeyValuePair<TKey, TValue>[]

The one-dimensional array of type KeyValuePair<TKey, TValue> that is the destination of the KeyValuePair<TKey, TValue> elements copied from the ICollection<T>. The array must have zero-based indexing.

index
Type: System.Int32

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

Implements

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

ExceptionCondition
ArgumentNullException

array is null.

ArgumentOutOfRangeException

index is less than 0.

ArgumentException

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

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

The following example shows how to use the ICollection<KeyValuePair<TKey, TValue>>.Add, ICollection<KeyValuePair<TKey, TValue>>.Contains, ICollection<KeyValuePair<TKey, TValue>>.CopyTo, and ICollection<KeyValuePair<TKey, TValue>>.Remove methods of the System.Collections.Generic.ICollection<T> generic interface to manipulate a Dictionary<TKey, TValue> object.

using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // 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"));

        Console.WriteLine();
        foreach( KeyValuePair<string, string> element in openWith )
        {
            Console.WriteLine("{0}, {1}", element.Key, element.Value);
        }

        // The Contains method also takes a KeyValuePair object. 
        //
        Console.WriteLine(
            "\nContains(KeyValuePair(\"txt\", \"notepad.exe\")): {0}", 
            openWith.Contains(new KeyValuePair<String,String>("txt", "notepad.exe")));

        // The Remove method takes a KeyValuePair object.) 
        // 
        // Use the Remove method to remove a key/value pair.
        Console.WriteLine("\nRemove(new KeyValuePair(\"dib\", \"paint.exe\"))");
        openWith.Remove(new KeyValuePair<String,String>("dib", "paint.exe"));

        Console.WriteLine();
        foreach( KeyValuePair<string, string> element in openWith )
        {
            Console.WriteLine("{0}, {1}", element.Key, element.Value);
        }

        // 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. 
        //
        Console.WriteLine();
        foreach( KeyValuePair<string, string> element in copy )
        {
            Console.WriteLine("{0}, {1}", element.Key, element.Value);
        }
    }
}

/* 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
 */

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, 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.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft