.NET
System.String.Copy
C# programming
string manipulation
software development

What's the use of System.String.Copy in .NET?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

System.String.Copy was historically used to create a separate string instance with identical content. In modern .NET, this API is obsolete and should generally be avoided. Because strings are immutable, most former use cases are better handled by assignment or explicit mutable structures.

Historical Purpose and Current Status

Older code used String.Copy to force a new object reference:

csharp
string original = "token-123";
string copy = string.Copy(original);

In current runtimes this method is obsolete, and reference-identity assumptions based on copy behavior are not reliable design choices.

More importantly, immutable strings do not need defensive cloning to prevent in-place mutation.

Why Assignment Is Usually Enough

For immutable data, assignment is safe and simple.

csharp
1using System;
2
3string original = "release-2026-03";
4string alias = original;
5
6Console.WriteLine(alias == original);
7Console.WriteLine(object.ReferenceEquals(alias, original));

Even if both references point to the same instance, neither variable can mutate shared text content.

Any transforming operation returns a new string:

csharp
1string input = "report.csv";
2string upper = input.ToUpperInvariant();
3
4Console.WriteLine(input);  // report.csv
5Console.WriteLine(upper);  // REPORT.CSV

Use Mutable Types When You Need Edits

Some legacy String.Copy usage actually indicates a need for mutable text processing. Use StringBuilder or char[] for explicit mutation workflows.

csharp
1using System;
2using System.Text;
3
4string source = "status: pending";
5var builder = new StringBuilder(source);
6builder.Replace("pending", "complete");
7
8string result = builder.ToString();
9Console.WriteLine(result);
csharp
1using System;
2
3string source = "abcde";
4char[] chars = source.ToCharArray();
5chars[0] = 'A';
6string edited = new string(chars);
7
8Console.WriteLine(edited);

These approaches make intent clear and avoid obsolete APIs.

Favor Value Comparison Rules

String logic should be based on value comparison semantics, not object identity.

csharp
1using System;
2
3string a = "Token";
4string b = "token";
5
6bool exact = string.Equals(a, b, StringComparison.Ordinal);
7bool ignoreCase = string.Equals(a, b, StringComparison.OrdinalIgnoreCase);
8
9Console.WriteLine(exact);
10Console.WriteLine(ignoreCase);

If old code used String.Copy to influence identity comparisons, that is usually a design smell that should be corrected.

Migrating Legacy Code

Migration is usually straightforward:

  1. Find String.Copy call sites.
  2. Replace with direct assignment where cloning was unnecessary.
  3. Replace with mutable structures where text editing is required.
  4. Remove identity-based logic that depends on copy side effects.
csharp
1// Old
2// string s2 = string.Copy(s1);
3
4// New
5string s2 = s1;

This reduces warnings and improves readability.

Performance and Maintainability

Removing obsolete APIs also avoids unnecessary allocations and warning suppressions. Clear string ownership and comparison contracts help prevent subtle bugs in caching, serialization, and security-sensitive normalization code.

Prefer explicit APIs with obvious semantics over historical patterns that no longer match modern runtime guidance.

Tooling and Analyzer Benefits

Replacing String.Copy also improves static-analysis signal quality. Modern analyzers and code-quality tools can enforce clearer string-comparison and allocation practices when obsolete API usage is removed. This reduces warning noise in CI and makes meaningful issues easier to spot during review. Cleaner diagnostics improve long-term maintainability, especially in large codebases with strict warning policies.

Common Pitfalls

  • Using String.Copy in new code despite obsolescence warnings.
  • Treating string reference identity as business logic.
  • Assuming immutable strings require defensive copying.
  • Using string APIs when mutable text buffers are actually needed.
  • Leaving outdated warning suppressions instead of modernizing call sites.

Summary

  • 'System.String.Copy is obsolete in modern .NET usage.'
  • Immutable strings make plain assignment safe for most scenarios.
  • Use StringBuilder or char[] for editable text workflows.
  • Compare strings by value with explicit comparison options.
  • Modernizing legacy copy calls improves clarity and reliability.

Course illustration
Course illustration

All Rights Reserved.