C# String Clone() Method (original) (raw)

Last Updated : 13 Mar, 2025

In C#, the Clone() method is a String method. It is used to clone the string object, which returns another copy of that data. In other words, it returns a reference to this instance of String. The return value will be only another view of the same data. The Clone() method is called directly on the current String instance. This method will not take any parameters. Some important characteristics of the clone method are,

**Example 1: Demonstration of String Clone() method.

csharp `

// C# program to illustrate // Clone() method using System; class Geeks { public static void Main(string[] args) { string s = "GeeksforGeeks";

    // Cannot implicitly convert 
    // type object to the string.
    // So explicit conversion 
    // using Clone() method
    string clone = (String)s.Clone();
   
    // Displaying both the string
    Console.WriteLine("Original String : {0}", s);
    Console.WriteLine("Clone String : {0}", clone);
}

}

`

Output

Original String : GeeksforGeeks Clone String : GeeksforGeeks

**Syntax of String Clone() Method

public object Clone()

**Return Type: This method returns a reference of an original string of type System.Object.

**Example 2: Demonstration of how the object created by the Clone() method shares the same reference (shallow copy).

C# `

using System;

class Geeks { public static void Main() { string s = "Hello, World!"; string s2 = "Hello, World!";

    // using the string clone method
    string Clone = (string)s.Clone();

    Console.WriteLine("Is s and s2 sharing the same memory location: {0}",
     object.ReferenceEquals(s, s2));
    Console.WriteLine("Is s and Clone sharing the same memory location: {0}",
     object.ReferenceEquals(s, Clone));
 
}

}

`

**Explanation: In the above example, we use the Clone() method to clone a string object. And check their memory reference. We know that the strings are immutable in C# so if we create another string with the same value it also shares the same reference but if we modify the Clone object it will not affect the original object because it will create a new string instance in that case.

**Important Points: