String.Clone() method with example in C#

C# String.Clone() strategy: Here, we will find out about the Clone() technique for String class with example.

C# String.Clone() Method

String.Clone() strategy is utilized to make a clone of a string object, it restores a reference to this example of a String.

Syntax:

    String String.Clone();

Parameter: None

Return value: It restores a reference to this occasion of a String.

Example:

    Input:
    string str1 = "Hello JustTechReview Readers";
    
    Function call:
    string str2 = (String)str1.Clone();

    Output:
    str1: Hello JustTechReview Readers
    str2: Hello JustTechReview Readers

C# Example to make a clone of a string utilizing String.Clone() strategy:

using System;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            //string variable
            string str1 = "Hello JustTechReview Readers";
            
            //making clone
            string str2 = (String)str1.Clone();

            //printing
            Console.WriteLine("str1: " + str1);
            Console.WriteLine("str2: " + str2);

            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output:

str1: Hello JustTechReview Readers
str2: Hello JustTechReview Readers

Leave a Comment

error: Alert: Content is protected!!