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
C# Copy() Method
The String.Copy() method in C# creates a new string instance with the same value as the specified string. While strings are immutable in C#, this method ensures you get a completely separate string object in memory, which can be useful in certain scenarios involving string interning.
Syntax
Following is the syntax for the String.Copy() method −
public static string Copy(string str);
Parameters
- str − The string to copy. Cannot be null.
Return Value
Returns a new string with the same value as the input string but as a separate object in memory.
Using String.Copy() for Basic Copying
Example
using System;
public class Demo {
public static void Main(string[] args) {
string s1 = "Amy";
string s2 = "Katie";
string s3 = String.Copy(s2);
Console.WriteLine("String1 = " + s1);
Console.WriteLine("String2 = " + s2);
Console.WriteLine("String3 (copy of s2) = " + s3);
Console.WriteLine("Are s1 and s2 equal? = " + s1.Equals(s2));
Console.WriteLine("Are s2 and s3 equal? = " + s2.Equals(s3));
Console.WriteLine("CompareOrdinal(s1, s2): " + string.CompareOrdinal(s1, s2));
Console.WriteLine("CompareOrdinal(s2, s3): " + string.CompareOrdinal(s2, s3));
}
}
The output of the above code is −
String1 = Amy String2 = Katie String3 (copy of s2) = Katie Are s1 and s2 equal? = False Are s2 and s3 equal? = True CompareOrdinal(s1, s2): -10 CompareOrdinal(s2, s3): 0
Understanding Object Independence
Example
using System;
public class Demo {
public static void Main(string[] args) {
string s1 = "Gary";
string s2 = "Gary";
string s3 = String.Copy(s2);
Console.WriteLine("String1 = " + s1);
Console.WriteLine("String2 = " + s2);
Console.WriteLine("String3 (copy) = " + s3);
Console.WriteLine("Is s1 and s2 equal? = " + s1.Equals(s2));
Console.WriteLine("Is s2 and s3 equal? = " + s2.Equals(s3));
// Reassign s3 to demonstrate independence
s3 = "Harry";
Console.WriteLine("After changing s3 to 'Harry':");
Console.WriteLine("Is s2 and s3 equal? = " + s2.Equals(s3));
Console.WriteLine("s2 remains: " + s2);
}
}
The output of the above code is −
String1 = Gary String2 = Gary String3 (copy) = Gary Is s1 and s2 equal? = True Is s2 and s3 equal? = True After changing s3 to 'Harry': Is s2 and s3 equal? = False s2 remains: Gary
When to Use String.Copy()
The String.Copy() method is rarely needed in modern C# programming because string assignment already creates independent references due to string immutability. However, it can be useful when working with string interning scenarios or when you specifically need to ensure a new string object in memory.
Example with Reference Comparison
using System;
public class Demo {
public static void Main(string[] args) {
string original = "Hello World";
string copied = String.Copy(original);
Console.WriteLine("Content comparison: " + original.Equals(copied));
Console.WriteLine("Reference comparison: " + ReferenceEquals(original, copied));
Console.WriteLine("Original: " + original);
Console.WriteLine("Copied: " + copied);
}
}
The output of the above code is −
Content comparison: True Reference comparison: False Original: Hello World Copied: Hello World
Conclusion
The String.Copy() method creates a new string instance with identical content but different memory location. While rarely needed due to string immutability in C#, it ensures complete object independence when required in specific scenarios involving string interning or memory management.
