The static method string.Copy() creates a new string object with the same characters as an existing string. It copies the entire string—not a portion. For everyday C# code, simple assignment often suffices because strings are immutable, but Copy() is useful when you explicitly need a separate instance.
01
Static Method
string.Copy(source)
02
New Instance
Separate object.
03
Full String
Not a substring.
04
vs Assignment
When each fits.
05
vs Clone()
New vs same ref.
06
Null Throws
ArgumentNullException.
Fundamentals
Definition and Usage
In C#, string.Copy() duplicates the full character sequence of a string into a newly allocated string object. It is declared as a static method: string.Copy(original).
Because C# strings are immutable, you cannot modify characters in place anyway—so many programs never call Copy() and simply write string copy = original. Use Copy() when you deliberately want a new object reference with identical text, or when mirroring API patterns from other .NET types.
💡
Beginner Tip
Copy() is not the same as Substring(). Substring(3, 10) extracts part of a string; Copy(text) duplicates the whole string. If you only need a piece, use Substring() or range syntax text[3..13].
Foundation
📝 Syntax
The single overload on System.String:
C#
public static string Copy(string str);
Parameters
str — the source string whose characters are copied. Must not be null.
Return Value
A new string containing the same characters as str. The original string is unchanged.
Cheat Sheet
⚡ Quick Reference
Goal
Code
Duplicate full string
string.Copy(source)
Share same text (usual case)
string copy = source
Extract a portion
source.Substring(3, 5)
Same instance (ICloneable)
(string)source.Clone()
Copy chars to array
source.CopyTo(0, buffer, 0, len)
Copy full
string.Copy(text)
New instance
Assign
string b = a
Most common
Substring
text.Substring(0, 5)
Partial text
CopyTo
text.CopyTo(...)
Into char[]
Hands-On
Examples Gallery
Run with dotnet run. Each example shows how Copy() works and how it differs from assignment, Clone(), and Substring().
📚 Getting Started
Duplicate a string with the static Copy() method.
Example 1 — Basic Copy() Usage
Create a duplicate of a greeting string and print both values.
C#
using System;
class Program {
static void Main() {
string original = "Hello ";
string duplicate = string.Copy(original);
Console.WriteLine($"Original: '{original}'");
Console.WriteLine($"Duplicate: '{duplicate}'");
Console.WriteLine($"Same text: {original == duplicate}");
}
}
📤 Output:
Original: 'Hello '
Duplicate: 'Hello '
Same text: True
How It Works
string.Copy reads every character from original and stores them in a new string object. Both variables show the same text, but they may refer to different objects in memory.
Example 2 — Copy() Creates a New Object
Use ReferenceEquals to see that Copy() can produce a different instance than the source.
C#
using System;
class Program {
static void Main() {
string original = new string(new char[] { 'H', 'i' });
string copied = string.Copy(original);
Console.WriteLine($"ReferenceEquals: {ReferenceEquals(original, copied)}");
Console.WriteLine($"Content equal: {original == copied}");
}
}
📤 Output:
ReferenceEquals: False
Content equal: True
How It Works
ReferenceEquals is false because Copy() allocated a new object. == compares character content and returns true. String interning may make literal comparisons share references, but Copy() from a freshly constructed string always creates a separate instance.
📈 Practical Patterns
Comparisons and choosing the right method.
Example 3 — Copy() vs Assignment
For immutable strings, assignment is usually the simpler choice.
C#
using System;
class Program {
static void Main() {
string message = "Welcome to C#";
string viaAssign = message;
string viaCopy = string.Copy(message);
Console.WriteLine($"Assign — same ref: {ReferenceEquals(message, viaAssign)}");
Console.WriteLine($"Copy — same ref: {ReferenceEquals(message, viaCopy)}");
Console.WriteLine($"Both readable: {viaAssign} | {viaCopy}");
}
}
📤 Output:
Assign — same ref: True
Copy — same ref: False
Both readable: Welcome to C# | Welcome to C#
How It Works
Assignment copies the reference, not the characters. Because strings cannot be mutated, that is safe for most code. Copy() forces a new object—useful only when you specifically need that separation.
Example 4 — Copy() vs Substring()
Copy() duplicates everything; Substring() extracts a slice.
C#
using System;
class Program {
static void Main() {
string text = "C# Programming";
string fullCopy = string.Copy(text);
string partial = text.Substring(3, 11); // "Programming"
Console.WriteLine($"Copy: '{fullCopy}'");
Console.WriteLine($"Substring: '{partial}'");
}
}
📤 Output:
Copy: 'C# Programming'
Substring: 'Programming'
How It Works
Copy(text) has no startIndex or length parameters. To grab characters from index 3 for 11 characters, use Substring(3, 11) or modern range syntax text[3..14].
Example 5 — Copy() vs Clone()
Clone() returns the same instance for strings; Copy() allocates anew.
C#
using System;
class Program {
static void Main() {
string original = "Hello, C#!";
object clonedObj = original.Clone();
string copied = string.Copy(original);
Console.WriteLine($"Clone same ref: {ReferenceEquals(original, clonedObj)}");
Console.WriteLine($"Copy same ref: {ReferenceEquals(original, copied)}");
Console.WriteLine($"Clone text: {(string)clonedObj}");
Console.WriteLine($"Copy text: {copied}");
}
}
📤 Output:
Clone same ref: True
Copy same ref: False
Clone text: Hello, C#!
Copy text: Hello, C#!
How It Works
Clone() implements ICloneable and, for immutable strings, returns this. Copy() is the explicit way to get a new string object with identical characters. See the Clone() tutorial for more detail.
Applications
🚀 Common Use Cases
Explicit duplication — when API design or tests require a distinct string instance.
Legacy .NET code — older libraries may call String.Copy by convention.
Defensive copies — rare for strings (immutable), but mirrors patterns used with mutable types.
Learning object references — comparing Copy() with assignment in tutorials.
Contrasting with CopyTo() — Copy() returns a string; CopyTo() writes into a char[].
The full length of str is measured—no partial range.
Measure
3
New string is allocated
Characters are copied into a fresh immutable string object.
Allocate
=
📄
Duplicate string returned
Same text, new reference. Original str is unchanged.
Important
📝 Notes
Copy() duplicates the entire string—not a range. Use Substring() for portions.
Passing null throws ArgumentNullException.
For most apps, string b = a is sufficient because strings are immutable.
Clone() on strings returns the same instance; Copy() creates a new one.
Do not confuse Copy() with CopyTo(), which copies characters into a character array.
String interning may cause literal assignments to share references—Copy() from a non-interned string avoids that sharing.
Performance
⚡ Optimization
Copy() allocates memory proportional to string length. Avoid calling it unnecessarily when assignment gives the same practical result. In tight loops over large collections, prefer passing strings by reference rather than copying each one with Copy(). For partial text, Substring() or spans may be more appropriate than copying the whole string first.
Wrap Up
Conclusion
The C# Copy() method creates a new string object with the same characters as the source. It is straightforward, rarely needed in everyday code, but important to distinguish from Substring(), Clone(), and CopyTo().
Practice the examples above, then continue to CopyTo() to learn how to copy string characters into a character array.
Use Copy() when Clone() semantics are what you need
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Copy()
Use these points whenever you duplicate strings in C#.
5
Core concepts
📄01
Static Method
string.Copy(source)
Basics
🔄02
New Instance
Separate object.
Return
📝03
Full String
Not Substring().
Scope
🔗04
vs Assignment
Usually enough.
Daily use
⚠️05
null Throws
Check first.
Safety
❓ Frequently Asked Questions
Copy() is a static method that creates a new string containing the same characters as the input string. Call it as string.Copy(source). It does not extract a portion—that is Substring(). Copy() duplicates the entire string into a new instance.
string.Copy(str) where str is the source string. Example: string duplicate = string.Copy("Hello"); There is only one overload—it takes a single string argument.
It returns a new string object with the same character sequence as the argument. The original string is never modified because C# strings are immutable.
For everyday use, string b = a is usually enough because strings are immutable—you cannot change a in place. Copy() explicitly allocates a new string object. Use Copy() when you need a separate instance; use assignment when you only need to refer to the same text.
Copy() duplicates the entire string into a new instance. Substring(startIndex) or Substring(startIndex, length) extracts part of a string. Copy() has no startIndex or length parameters.
string.Copy(null) throws ArgumentNullException. Always validate with string.IsNullOrEmpty() or a null check before calling Copy() on user-provided input.
Did you know?
The .NET runtime interns string literals—identical literal text in your source code may share one object in memory. That is why ReferenceEquals("Hi", "Hi") can be true, while ReferenceEquals(string.Copy("Hi"), "Hi") is typically false. Content comparison with == still works either way.