
Strings rarely need copying. There are some cases in the C# language where they do. The language provides the string.Copy method. This method is not useful often. It rarely has a place when using string interning. It is important to know its utility.
This C# example program uses the string.Copy method to copy a string's entire data.

The string.Copy method is found on the System.String type in the base class library System namespace. You can use the "string" type in the C# language because it is aliased. The string.Copy method internally calls into unsafe code that allocates and copies strings. This example doesn't do anything useful, but shows the functionality of System.String.Copy.
Program that uses Copy string [C#]
using System;
class Program
{
static void Main()
{
//
// Copy a literal string.
//
string value1 = "Literal";
string value2 = string.Copy(value1);
//
// Write the string values.
//
Console.WriteLine(value1);
Console.WriteLine(value2);
//
// See if the references are equal.
//
Console.WriteLine(object.ReferenceEquals(value1, value2));
}
}
Output
Literal
Literal
False
Description. The program defines a Main entry point, which is where control flow begins. Two string variables are declared, and they are assigned to a string literal and the result of a method invocation to the string.Copy method. These two values are then printed to the screen.
Console.WriteLineMethod usage. The string.Copy method is aliased to the System.String.Copy method, and it accepts one parameter of type string. It returns a new string containing the same string data as the source string. The object data is equivalent in both objects, but the data is not in the same storage location and the references are unequal.
ReferenceEquals. The ReferenceEquals method is rarely used on strings and shows you whether the two strings point to the same object data. Its result does not indicate if the actual object data is equal or not. Recall that string itself is a reference type, which points to the char buffer data in the heap.
object.ReferenceEquals Method
Internally, the string.Copy method first checks for null arguments, and enters an unsafe context which calls the internal wstrcpyPtrAligned method. This method copies the bytes and characters from one string to another. The loop in this method is highly optimized and unrolled.

Some tutorials state that the string.Copy method's result is the same as an assignment of string variables. However, in the C# programming language the assignment operator is guaranteed to do a bitwise copy of the storage location of the variable. Additionally, there are no user-defined overloads of the assignment operator. For this reason, assigning a string variable to another variable is much faster than invoking string.Copy.

We looked at the string.Copy method in the C# programming language. We noted how it copies the internal data stored in the managed heap for the string, but does not change the reference by assignment. Internally, the method actually copies the characters in the string. This method is not normally needed and is best avoided for regular programs.
String Type