Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to copy a String into another String in C#
In C#, there are several ways to copy a string into another string. Strings in C# are immutable, meaning they cannot be changed after creation. When you "copy" a string, you create a new string object with the same value.
Syntax
Following are the common syntaxes for copying strings in C# −
// Using String.Copy() method string str2 = String.Copy(str1); // Using direct assignment string str2 = str1; // Using string constructor string str2 = new string(str1.ToCharArray());
Using String.Copy() Method
The String.Copy() method creates a new string object with the same value as the source string −
using System;
public class Demo {
static public void Main(){
string str1 = "Kevin";
string str2 = String.Copy(str1);
Console.WriteLine("String1 = " + str1);
Console.WriteLine("String2 = " + str2);
Console.WriteLine("Are they the same reference? " + ReferenceEquals(str1, str2));
}
}
The output of the above code is −
String1 = Kevin String2 = Kevin Are they the same reference? False
Using Direct Assignment
Due to string interning in C#, direct assignment often results in the same reference for identical string literals −
using System;
public class Demo {
static public void Main(){
string str1 = "Maisie";
string str2 = "Ryan";
Console.WriteLine("String1 (Before copying) = " + str1);
Console.WriteLine("String2 (Before copying) = " + str2);
str2 = str1; // Direct assignment
Console.WriteLine("String1 = " + str1);
Console.WriteLine("String2 (Updated) = " + str2);
Console.WriteLine("Are they the same reference? " + ReferenceEquals(str1, str2));
}
}
The output of the above code is −
String1 (Before copying) = Maisie String2 (Before copying) = Ryan String1 = Maisie String2 (Updated) = Maisie Are they the same reference? True
Using String Constructor
You can also create a new string by converting the original string to a character array −
using System;
public class Demo {
static public void Main(){
string str1 = "Hello World";
string str2 = new string(str1.ToCharArray());
Console.WriteLine("String1 = " + str1);
Console.WriteLine("String2 = " + str2);
Console.WriteLine("Are they equal? " + str1.Equals(str2));
Console.WriteLine("Are they the same reference? " + ReferenceEquals(str1, str2));
}
}
The output of the above code is −
String1 = Hello World String2 = Hello World Are they equal? True Are they the same reference? False
Comparison of String Copying Methods
| Method | Creates New Object | Performance | Use Case |
|---|---|---|---|
| Direct Assignment | Usually No (due to interning) | Fastest | General purpose copying |
| String.Copy() | Always Yes | Moderate | When you need guaranteed new instance |
| String Constructor | Always Yes | Slowest | When working with character arrays |
Conclusion
In most scenarios, direct assignment (str2 = str1) is sufficient for copying strings in C#. Use String.Copy() when you specifically need a new string instance, though this is rarely necessary due to string immutability.
