C# | CopyTo() Method
In C#, CopyTo() method is a string method. It is used to copy a specified number of characters from a specified position in the string and it copies the characters of this string into a array of Unicode characters.
Syntax:
public void CopyTo(int sourceIndex, char[] destination,
int destinationIndex, int count)
Explanation: CopyTo() method will copied the count characters from the sourceIndex position to the destinationIndex position of destination character array. This method accepts four parameters as follows :
- sourceIndex : Index of String to be copied. And its type is System.Int32 .
- destination : It is the array of Unicode characters to which characters will be copied. Its type is System.Char[].
- destinationIndex : It is starting index of array from where the copy operation begins. Its type is System.Int32.
- count : It is the number of characters which will copy to destination. Its type is System.Int32.
Examples :
Input : str = "GeeksForGeeks"
char [] Copystring = new char[15];
str.CopyTo(5, Copystring, 0, 3);
Output: For
Input : str2 = "GeeksForGeeks";
char [] Copystring = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'};
str2.CopyTo(8, Copystring, 6, 5);
Output: Hello Geeks
Below example programs illustrate the ToCopy() Method :
- Example 1:
// C# program to illustrate the// ToCopy() string methodusingSystem;classGeeks {// Main MethodpublicstaticvoidMain(){stringstr ="GeeksForGeeks";char[] dest =newchar[15];// str index 5 to 5+3 has to// copy into Copystring// 3 is no. of character// 0 is start index of Copystringstr.CopyTo(5, dest, 0, 3);// Displaying StringConsole.Write("The Copied String in dest Variable is: ");Console.WriteLine(dest);}}chevron_rightfilter_noneOutput:The Copied String in dest Variable is: For
- Example 2:
// C# program to illustrate the// ToCopy() string methodusingSystem;classGeeks {// Main MethodpublicstaticvoidMain(){stringstr2 ="GeeksForGeeks";char[] dest = {'H','e','l','l','o',' ','W','o','r','l','d'};// str index 8 to 8 + 5 has// to copy into Copystring// 5 is no of character// 6 is start index of Copystringstr2.CopyTo(8, dest, 6, 5);// Displaying the resultConsole.Write("String Copied in dest is: ");Console.WriteLine(dest);}}chevron_rightfilter_noneOutput:String Copied in dest is: Hello Geeks
Note: If “destination” is null then it will cause an Exception as ArgumentNullException. There are different cases when exception occurs: ArgumentOutOfRangeException
cases 1: if sourceIndex, destinationIndex, or count is negative.
cases 2: if sourceIndex does not identify a position in the current instance.
cases 3: if destinationIndex does not identify a valid index in the destination array.
cases 4: if the count is greater than the length of the substring from startIndex to the end of this instance
cases 5: if the count is greater than the length of the subarray from destinationIndex to the end of the destination array.
Reference: https://msdn.microsoft.com/en-us/library/system.string.copyto
Recommended Posts:
- Queue.CopyTo() Method in C#
- Stack.CopyTo() Method in C#
- StringBuilder.CopyTo Method in C#
- Difference between Method Overriding and Method Hiding in C#
- C# | Join() Method | Set - 1
- C# | String.Contains() Method
- C# | PadLeft() Method
- C# | ToLower() Method
- Method Hiding in C#
- C# | ToUpper() Method
- C# | Copy() Method
- C# | Dictionary.Add() Method
- C# | IndexOfAny() Method
- C# | Uri.FromHex() Method
- C# | Uri.GetHashCode() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



