StringBuilder.CopyTo Method (Int32, Char[], Int32, Int32)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Copies the characters from a specified segment of this instance to a specified segment of a destination Char array.

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

[<ComVisibleAttribute(false)>]
member CopyTo : 
        sourceIndex:int *
        destination:char[] *
        destinationIndex:int *
        count:int -> unit

Parameters

sourceIndex
Type: System.Int32

The starting position in this instance where characters will be copied from. The index is zero-based.

destination
Type: System.Char[]

The array where characters will be copied.

destinationIndex
Type: System.Int32

The starting position in destination where characters will be copied. The index is zero-based.

count
Type: System.Int32

The number of characters to be copied.

Exception Condition
ArgumentNullException

destination is null.

ArgumentOutOfRangeException

sourceIndex, destinationIndex, or count, is less than zero.

-or-

sourceIndex is greater than the length of this instance.

ArgumentException

sourceIndex + count is greater than the length of this instance.

-or-

destinationIndex + count is greater than the length of destination.

The CopyTo method is intended to be used in the rare situation when you need to efficiently copy successive sections of a StringBuilder object to an array. The array should be a fixed size, preallocated, reusable, and possibly globally accessible.

For example, your application could populate a StringBuilder object with a large number of characters then use the CopyTo method to copy small, successive pieces of the StringBuilder object to an array where the pieces are processed. When all the data in the StringBuilder object is processed, the size of the StringBuilder object is set to zero and the cycle is repeated.

The following example demonstrates the CopyTo method.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top
Show: