Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

String.CopyTo Method

Updated: December 2009

Copies a specified number of characters from a specified position in this instance to a specified position in an array of Unicode characters.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public void CopyTo(
	int sourceIndex,
	char[] destination,
	int destinationIndex,
	int count
)

Parameters

sourceIndex
Type: System.Int32

The index of the first character in this instance to copy.

destination
Type: System.Char[]

An array of Unicode characters to which characters in this instance are copied.

destinationIndex
Type: System.Int32

An index in destination at which the copy operation begins.

count
Type: System.Int32

The number of characters in this instance to copy to destination.

ExceptionCondition
ArgumentNullException

destination is null.

ArgumentOutOfRangeException

sourceIndex, destinationIndex, or count is negative

-or-

count is greater than the length of the substring from startIndex to the end of this instance

-or-

count is greater than the length of the subarray from destinationIndex to the end of destination

count characters are copied from the sourceIndex position of this instance to the destinationIndex position of destination.

sourceIndex and destinationIndex are zero-based.

The following example demonstrates the CopyTo method.

using System;

public class CopyToTest {
    public static void Main() {

        // Embed an array of characters in a string
        string strSource = "changed";
    char [] destination = { 'T', 'h', 'e', ' ', 'i', 'n', 'i', 't', 'i', 'a', 'l', ' ',
                'a', 'r', 'r', 'a', 'y' };

        // Print the char array
        Console.WriteLine( destination );

        // Embed the source string in the destination string
        strSource.CopyTo ( 0, destination, 4, strSource.Length );

        // Print the resulting array
        Console.WriteLine( destination );

        strSource = "A different string";

        // Embed only a section of the source string in the destination
        strSource.CopyTo ( 2, destination, 3, 9 );

        // Print the resulting array
        Console.WriteLine( destination );
    }
}
// The example displays the following output: 
//       The initial array 
//       The changed array 
//       Thedifferentarray

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

December 2009

Revised the parameter descriptions.

Customer feedback.

Community Additions

Show:
© 2014 Microsoft. All rights reserved.