C# String Clone() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

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.

C#
object copy = text.Clone();
CallWhat 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
C#
public object Clone();

Parameters

This method has no parameters.

Return Value

Return ValueDescription
objectThe same string instance, typed as object.

C# Examples

Example 1 — Basic Clone()

Call Clone(), cast to string, and print the text.

C#
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}");
    }
}

How It Works

  1. Call the instance method. original.Clone() returns object.
  2. Cast to string. (string) lets you use string members again.
  3. Compare with ==. Content equality is true.
  4. 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.

C#
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}");
    }
}

How It Works

  1. Assign. assigned = a points at the same string.
  2. Clone. Also returns that same instance (as object).
  3. Copy. Allocates a new string with the same characters.
  4. 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.

C#
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");
        }
    }
}

How It Works

  1. Direct cast. (string)s.Clone() converts the object result.
  2. as operator. Same successful cast when the value is a string.
  3. Null receiver. Instance call on null throws NullReferenceException.
  4. 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.

Next: Compare()

Learn how to compare two strings and get an ordering result.

Compare() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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