C# String Copy

Description

String Copy creates a new instance of String with the same value as a specified String.

Syntax

String.Copy has the following syntax.


public static string Copy(
  string str
)

Parameters

String.Copy has the following parameters.

  • str - The string to copy.

Returns

String.Copy method returns A new string with the same value as str.

Example

The following code shows how to use String.Copy method.


//from   ww w. ja  v  a 2s.  com
using System;

class Example
{
   public static void Main() 
   {
      string str1 = "abc";
      string str2 = "xyz";

      Console.WriteLine("str1 = '{0}'", str1);
      Console.WriteLine("str2 = '{0}'", str2);

      str2 = String.Copy(str1);
      Console.WriteLine("ReferenceEquals: {0}", Object.ReferenceEquals(str1, str2));
      Console.WriteLine("Equals: {0}", Object.Equals(str1, str2));

      str2 = str1;
      Console.WriteLine("ReferenceEquals: {0}", Object.ReferenceEquals(str1, str2));
      Console.WriteLine("Equals: {0}", Object.Equals(str1, str2));
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version