C# Tutorial C# Advanced C# References

C# String - Clone() Method



The C# Clone() method returns a object that is a copy of the given string. The return value is not an independent copy of this instance, it is simply another view of the same data.

Syntax

public object Clone();

Parameters

No parameter is required.

Return Value

Returns the instance of the string object.

Exception

NA.

Example:

In the example below, Clone() method returns the instance of the string called MyStr.

using System;

class MyProgram {
  static void Main(string[] args) {
    string MyStr = "Hello";

    //convert object into string
    string NewStr = (String)MyStr.Clone();

    Console.WriteLine(NewStr);
  }
}

The output of the above code will be:

Hello

❮ C# String Methods

5