C# String Clone() Method

Definition
In C#, Clone() returns the same string instance boxed as an object. Strings are immutable, so cloning does not create new text.
You call it on a string with no parameters. The result is an object that still refers to the same string. A normal assignment is usually enough.
Syntax
Call Clone on the string. Start with this line.
object copy = text.Clone();
| Call | What it does |
|---|
text.Clone() | Returns the same string instance as an object. |
This method comes from ICloneable. Prefer a simple assignment when you only need another variable.
Library declarations
Parameters
This method has no parameters.
Return Value
| Return Value | Description |
|---|
| object | The same string instance, typed as object. |
C# Examples
Example 1 — Basic Clone()
Call Clone(), cast to string, and print the text.
using System;
class Program {
static void Main() {
string original = "Hello, C#";
string cloned = (string)original.Clone();
Console.WriteLine($"Original: {original}");
Console.WriteLine($"Cloned: {cloned}");
Console.WriteLine($"Same text: {original == cloned}");
}
}
Original: Hello, C#
Cloned: Hello, C#
Same text: True
How It Works
- Call the instance method.
original.Clone() returns object. - Cast to string.
(string) lets you use string members again. - Compare with ==. Content equality is
true. - No new text buffer. For strings, Clone reuses the same instance.
Example 2 — Clone vs Copy vs Assignment
Assignment and Clone() share the object; Copy() creates a new one.
using System;
class Program {
static void Main() {
string a = "CodeToFun";
string assigned = a;
string cloned = (string)a.Clone();
string copied = string.Copy(a);
Console.WriteLine($"assign same object? {object.ReferenceEquals(a, assigned)}");
Console.WriteLine($"clone same object? {object.ReferenceEquals(a, cloned)}");
Console.WriteLine($"copy same object? {object.ReferenceEquals(a, copied)}");
Console.WriteLine($"copy == a (text)? {copied == a}");
}
}
assign same object? True
clone same object? True
copy same object? False
copy == a (text)? True
How It Works
- Assign.
assigned = a points at the same string. - Clone. Also returns that same instance (as
object). - Copy. Allocates a new string with the same characters.
- Text still matches.
copied == a is true even when identity differs.
Example 3 — Cast and as
Practice both cast styles, and guard a null receiver.
using System;
class Program {
static void Main() {
string s = "Safe";
string casted = (string)s.Clone();
string withAs = s.Clone() as string;
Console.WriteLine($"cast: {casted}");
Console.WriteLine($"as: {withAs}");
string missing = null;
try {
_ = missing.Clone();
} catch (NullReferenceException) {
Console.WriteLine("null.Clone() throws NullReferenceException");
}
}
}
cast: Safe
as: Safe
null.Clone() throws NullReferenceException
How It Works
- Direct cast.
(string)s.Clone() converts the object result. - as operator. Same successful cast when the value is a string.
- Null receiver. Instance call on
null throws NullReferenceException. - Validate first. Check the string before calling
Clone().
Frequently Asked Questions
Clone() returns the same string instance as an object. It does not create new text.
No. Call it on the string with empty parentheses.
An object that still refers to the same string instance.
Prefer a normal assignment when you only need another variable that holds the same string.
Calling Clone() on null throws NullReferenceException.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful