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: 22-Jun-2020

131 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements