C# String Clone() Method

You are Here:

C# String Clone()

The Clone() method is used to clone the string object, which returns another copy of same data.

The return type of Clone() method is object.

Note: This type of cloning is called shallow copy, i.e, any changes made to any string object will not affect the other.

Example

C# Compiler
using System; namespace myApp { class Program { static void Main(string[] args) { string str1 = "wikimass.com"; string str2 = (String)str1.Clone(); // Displaying both the string Console.WriteLine("String: {0}", str1); Console.WriteLine("Clone String: {0}", str2); } } }

Output

String: wikimass.com Clone String: wikimass.com

Syntax

public String (String)str1.Clone()

Parameter Value

This method does not take any parameter.

Return Value

ValueExplanation
ObjectReturns a reference to this instance of String.

More Example

In the following example, any changes made to Actual string (s1) will not affect the clone string (s2).

Example

C# Compiler
using System; namespace myApp { class Program { static void Main(string[] args) { string str1 = "wikimass.com"; string str2 = (String)str1.Clone(); // Changing str1 value. str1 = "wikipedia.com"; // Displaying both the string Console.WriteLine("String: {0}", str1); Console.WriteLine("Clone String: {0}", str2); } } }

Output

String: wikipedia.com Clone String: wikimass.com

Reminder

Hi Developers, we almost covered 90% of String functions and Interview Question on C# with examples for quick and easy learning.

We are working to cover every Single Concept in C#.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author