System.String.Copy Method

Creates a new instance of string with the same value as a specified string.

Syntax

public static string Copy (string str)

Parameters

str
The string to copy.

Returns

A new string with the same value as str.

Exceptions

TypeReason
ArgumentNullException str is a null reference.

Remarks

The string.Copy(string) method returns a string object that has the same value as the original string but represents a different object reference. It differs from an assignment operation, which assigns an existing string reference to an additional object variable. The example illustrates the difference.

Example

The following example demonstrates copying strings.

C# Example

using System;
public class StringCopyExample {
 public static void Main() {
 string strA = "string";
 Console.WriteLine( "The initial string, strA, is '{0}'.", strA );
 string strB = String.Copy( strA );
 strA = strA.ToUpper();
 Console.WriteLine( "The copied string, strB, before strA.ToUpper, is '{0}'.", strB );
 Console.WriteLine( "The initial string after StringCopy and ToUpper, is '{0}'.", strA );
 Console.WriteLine( "The copied string, strB, after strA.ToUpper, is '{0}'.", strB );
 }
}
   

The output is

The initial string, strA, is 'string'.
The copied string, strB, before strA.ToUpper, is 'string'.
The initial string after StringCopy and ToUpper, is 'STRING'.
The copied string, strB, after strA.ToUpper, is 'string'.

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0