C# - String.Copy() Method with Example

Given a string object and we have to create another instance of this object using String.Copy() method in C#.
[Last updated : March 20, 2023]

String.Copy() Method

It is a method of String class which is used to create a new instance of String class copy value of already created String.

Syntax

String String.Copy(String str);

Return Value

Above method return newly created instance with value of passed string.

C# program to demonstrate the example of String.Copy() method

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1 {
  class Program {
    static void Main() {
      String str1;
      String str2;

      Console.Write("Enter string : ");
      str1 = Console.ReadLine();
      str2 = String.Copy(str1);

      Console.WriteLine("Value is str1 : " + str1);
      Console.WriteLine("Value of str2 : " + str2);
    }
  }
}

Output

Enter string : Hello how are you
Value is str1 : Hello how are you
Value of str2 : Hello how are you

Here, we are declare a string object str1 and assigning the string value by reading from console, then we created another object of string str2 using String.Copy(), where str2 is the instance of str1 with the same value assigned in str1.

C# Basic Programs »

Comments and Discussions!

Load comments ↻





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