C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Variables: Two string variables are assigned to a string literal, and to the result of string.Copy.
ConsoleAlias: The string.Copy method is aliased to the System.String.Copy method, and it accepts one parameter of type string.
Info: The object data is equivalent in both objects, but the data is not in the same storage location and the references are unequal.
ReferenceEquals: This shows whether the 2 strings point to the same object data. It does not indicate if the object data is equal or not.
object.ReferenceEqualsC# program that uses string.Copy method
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
Copies: This method copies the bytes and characters from one string to another.
And: The loop in this method is highly optimized and unrolled. It would be hard to develop a faster one.
Also: There are no user-defined overloads of the assignment operator in the C# language.
So: Assigning a string variable to another variable is much faster than invoking string.Copy.