What is the difference between String.Copy() and String.CopyTo() methods in C#?



String.CopyTo() method gets the string characters and places them into an array. A group of characters are copied from source string into a character array.

The following is the Copy() method −

Example

 Live Demo

using System; class Demo {    static void Main(String[] args) {       string str = "This is it!";       char[] ch = new char[5];       str.CopyTo(2, ch, 0, 2);       Console.WriteLine("Output...");       Console.WriteLine(ch);    } }

Output

Output... is

String.Copy() creates a new string object with similar content.

Example

 Live Demo

using System; class Demo {    static void Main(String[] args) {       string str1 = "Welcome!";       string str2 = "user";       str2 = String.Copy(str1);       Console.WriteLine("Output...");       Console.WriteLine(str2);    } }

Output

Output... Welcome!
Updated on: 2020-06-22T07:22:38+05:30

343 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements