Skip to main content
.NET Framework Class Library
StringCopy Method

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

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)
Syntax
Public Shared Function Copy ( _
	str As [%$TOPIC/dabb67x2_en-us_VS_110_3_0_0_0_0%] _
) As [%$TOPIC/dabb67x2_en-us_VS_110_3_0_0_0_1%]
public static [%$TOPIC/dabb67x2_en-us_VS_110_3_0_1_0_0%] Copy(
	[%$TOPIC/dabb67x2_en-us_VS_110_3_0_1_0_1%] str
)
public:
static [%$TOPIC/dabb67x2_en-us_VS_110_3_0_2_0_0%]^ Copy(
	[%$TOPIC/dabb67x2_en-us_VS_110_3_0_2_0_1%]^ str
)
static member Copy : 
        str:[%$TOPIC/dabb67x2_en-us_VS_110_3_0_3_0_0%] -> [%$TOPIC/dabb67x2_en-us_VS_110_3_0_3_0_1%]

Parameters

str
Type: SystemString

The string to copy.

Return Value

Type: SystemString
A new string with the same value as str.
Exceptions
ExceptionCondition
ArgumentNullException

str is .

Remarks

The Copy 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.

Examples

The following example creates two string objects with different values. When it calls the Copy method to assign the first value to the second string, the output indicates that the strings represent different object references although their values are now equal. On the other hand, when the first string is assigned to the second string, the two strings have identical values because they represent the same object reference.

Module Example
   Public Sub Main()
      Dim str1 As String = "abc" 
      Dim str2 As String = "xyz"

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

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

      Console.WriteLine("After Assignment...")
      str2 = str1
      Console.WriteLine("str1 = '{0}'", str1)
      Console.WriteLine("str2 = '{0}'", str2)
      Console.WriteLine("ReferenceEquals: {0}", Object.ReferenceEquals(str1, str2))
      Console.WriteLine("Equals: {0}", Object.Equals(str1, str2))
   End Sub 
End Module 
' The example displays the following output: 
'       str1 = 'abc' 
'       str2 = 'xyz' 
'        
'       After String.Copy... 
'       str1 = 'abc' 
'       str2 = 'abc' 
'       ReferenceEquals: False 
'       Equals: True 
'        
'       After Assignment... 
'       str1 = 'abc' 
'       str2 = 'abc' 
'       ReferenceEquals: True 
'       Equals: True
using System;

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

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

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

      Console.WriteLine("\nAfter Assignment...");
      str2 = str1;
      Console.WriteLine("str1 = '{0}'", str1);
      Console.WriteLine("str2 = '{0}'", str2);
      Console.WriteLine("ReferenceEquals: {0}", Object.ReferenceEquals(str1, str2));
      Console.WriteLine("Equals: {0}", Object.Equals(str1, str2));
   }
}
// The example displays the following output: 
//       str1 = 'abc' 
//       str2 = 'xyz' 
//        
//       After String.Copy... 
//       str1 = 'abc' 
//       str2 = 'abc' 
//       ReferenceEquals: False 
//       Equals: True 
//        
//       After Assignment... 
//       str1 = 'abc' 
//       str2 = 'abc' 
//       ReferenceEquals: True 
//       Equals: True
Version Information

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1
Platforms

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.