C# String - Clone() Method



The C# String Clone() method is used to shallow copy of the string object. However, It's important to note that the clone method does not create a new string. It simply creates another view of the same data.

A shallow copy is a new object that shares the same instance variable as an existing object.

Syntax

Following is the syntax of the C# string Clone() method −

public object Clone();

Parameters

This method does not accept any parameters −

Return value

This method returns instance of String.

Example 1

This is the basic example of the string Clone() method. We can use this method to create another view of the same data.

    
using System;
class Program {
   static void Main() {
      string original_string = "Hello, TutorialsPoint !";
      object cloned_string = original_string.Clone();

      Console.WriteLine($"Original: {original_string}");
      Console.WriteLine($"Cloned: {cloned_string}");
      Console.WriteLine($"Are both references equal? {ReferenceEquals(original_string, cloned_string)}");
   }
}

Output

Following is the output −

Original: Hello, TutorialsPoint !
Cloned: Hello, TutorialsPoint !
Are both references equal? True

Example 2: Modify The String

Let us see another example of the Clone() method. Here, we modify the original string after cloning −

using System;
class Program {
   static void Main() {
      string original_str = "Learning C# in tutorialspoint is fun";
      object cloned_str = original_str.Clone();

      Console.WriteLine("Original String: " + original_str);
      Console.WriteLine("Cloned String: " + cloned_str);

      // Modify the original string
      original_str = "I love tutorialspoint";

      Console.WriteLine("\nAfter modifying the original string:");
      Console.WriteLine("Original String: " + original_str);
      Console.WriteLine("Cloned String: " + cloned_str);
   }
}

Output

Following is the output −

Original String: Learning C# in tutorialspoint is fun
Cloned String: Learning C# in tutorialspoint is fun

After modifying the original string:
Original String: I love tutorialspoint
Cloned String: Learning C# in tutorialspoint is fun

Example 3: Modify The Cloned String

Here, in this example we modified the cloned string −

using System;
class Program {
   static void Main() {
      string original = "Hii, TutorialsPoint!";
      object cloned = original.Clone();

      Console.WriteLine("Original String: " + original);
      Console.WriteLine("Cloned String: " + cloned);
      
      string modified_cloned = ((string)cloned).ToUpper();

       Console.WriteLine("\nAre the references equal? " + ReferenceEquals(original, cloned));

      Console.WriteLine("\nAfter modifying the cloned string:");
      Console.WriteLine("Modified Cloned" + modified_cloned);
   }
}

Output

Following is the output −

Original String: Hii, TutorialsPoint!
Cloned String: Hii, TutorialsPoint!

Are the references equal? True

After modifying the cloned string:
Modified ClonedHII, TUTORIALSPOINT!
csharp_strings.htm
Advertisements