find and solve || findandsolve.com
Please wait.....

C# String Clone

This is useful since string implements ICloneable, so we can create a copy of clones for a collection of ICloneable items. This is boring when the collection is of strings only, but it's useful when the collection contains multiple types that implement ICloneable.In c# clone method is used to clone and return a copy of the specified string object.

C# String Clone Method Syntax

public object Clone()

Parameters

It does not take any parameter.

Returns

It returns a reference.


C# String Clone Method Example

In the given below using a string Clone() method to clone a specified string object and return a copy of the same data in the c# programming language.

using System;
using System.Text.RegularExpressions;
public class Program
{
 public static void Main()
 {
        string message= "This is Clone Example in c#";
          string message1= (string)message.Clone();
          Console.WriteLine("String: {0}", message);
          Console.WriteLine("Clone String: {0}", message1);
          Console.WriteLine("Reference Equals: {0}", Object.ReferenceEquals(message, message1));
          Console.WriteLine("\nPress Enter Key to Exit..");
          Console.ReadLine();
  }
}
output
String: This is Clone Example in c#
Clone String: This is Clone Example in c#
Reference Equals: True

Another Example

    using System;
    public class StringExample
    {
        public static void Main(string[] args)
        {
             string message= "Hello ";
             string message1= (String)message.Clone();
             Console.WriteLine(message);
             Console.WriteLine(message2);         }
    }    
output
Hello
Hello