Home » C#.Net

String.Clone() method with example in C#

C# String.Clone() method: Here, we are going to learn about the Clone() method of String class with example.
Submitted by IncludeHelp, on March 17, 2019

C# String.Clone() Method

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: 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

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.