C# String.Clone() Method with Example

In this tutorial, we will learn about the String.Clone() method with its usage, syntax, and examples. By IncludeHelp Last updated : April 07, 2023

C# String.Clone() Method

The String.Clone() method is used to make a clone of a string object, it returns a reference to this instance of a String.

Syntax

String String.Clone();

Parameter(s)

  • None

Return Value

It returns a reference to this instance of a String.

Example

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

Output:
str1: Hello IncludeHelp Readers
str2: Hello IncludeHelp Readers

C# example to make a clone of a string using String.Clone() method

using System;
using System.Text;

namespace Test {
  class Program {
    static void Main(string[] args) {
      //string variable
      string str1 = "Hello IncludeHelp 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 IncludeHelp Readers
str2: Hello IncludeHelp Readers

Reference: String.Clone Method




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.