Array::Clone Method
Creates a shallow copy of the Array.
Assembly: mscorlib (in mscorlib.dll)
A shallow copy of an Array copies only the elements of the Array, whether they are reference types or value types, but it does not copy the objects that the references refer to. The references in the new Array point to the same objects that the references in the original Array point to.
In contrast, a deep copy of an Array copies the elements and everything directly or indirectly referenced by the elements.
The clone is of the same Type as the original Array.
This method is an O(n) operation, where n is Length.
The following code example clones a System.Globalization::CultureInfo array and demonstrates the behavior of a shallow copy.
using namespace System; using namespace System::Globalization; void PrintIndexAndValues( Array^ myArray ); int main() { // Create and initialize a new CultureInfo array. CultureInfo^ ci0 = gcnew CultureInfo( "ar-SA",false ); CultureInfo^ ci1 = gcnew CultureInfo( "en-US",false ); CultureInfo^ ci2 = gcnew CultureInfo( "fr-FR",false ); CultureInfo^ ci3 = gcnew CultureInfo( "ja-JP",false ); array<CultureInfo^>^arrCI = {ci0,ci1,ci2,ci3}; // Create a clone of the CultureInfo array. array<CultureInfo^>^arrCIClone = (array<CultureInfo^>^)arrCI->Clone(); // Replace an element in the clone array. CultureInfo^ ci4 = gcnew CultureInfo( "th-TH",false ); arrCIClone[ 0 ] = ci4; // Display the contents of the original array. Console::WriteLine( "The original array contains the following values:" ); PrintIndexAndValues( arrCI ); // Display the contents of the clone array. Console::WriteLine( "The clone array contains the following values:" ); PrintIndexAndValues( arrCIClone ); // Display the DateTimeFormatInfo.DateSeparator for the fourth element in both arrays. Console::WriteLine( "Before changes to the clone:" ); Console::WriteLine( " Original: The DateTimeFormatInfo->DateSeparator for {0} is {1}.", arrCI[ 3 ]->Name, arrCI[ 3 ]->DateTimeFormat->DateSeparator ); Console::WriteLine( " Clone: The DateTimeFormatInfo->DateSeparator for {0} is {1}.", arrCIClone[ 3 ]->Name, arrCIClone[ 3 ]->DateTimeFormat->DateSeparator ); // Replace the DateTimeFormatInfo.DateSeparator for the fourth element in the clone array. arrCIClone[ 3 ]->DateTimeFormat->DateSeparator = "-"; // Display the DateTimeFormatInfo.DateSeparator for the fourth element in both arrays. Console::WriteLine( "After changes to the clone:" ); Console::WriteLine( " Original: The DateTimeFormatInfo->DateSeparator for {0} is {1}.", arrCI[ 3 ]->Name, arrCI[ 3 ]->DateTimeFormat->DateSeparator ); Console::WriteLine( " Clone: The DateTimeFormatInfo->DateSeparator for {0} is {1}.", arrCIClone[ 3 ]->Name, arrCIClone[ 3 ]->DateTimeFormat->DateSeparator ); } void PrintIndexAndValues( Array^ myArray ) { for ( int i = myArray->GetLowerBound( 0 ); i <= myArray->GetUpperBound( 0 ); i++ ) Console::WriteLine( "\t[{0}]:\t{1}", i, myArray->GetValue( i ) ); } /* This code produces the following output. The original array contains the following values: [0]: ar-SA [1]: en-US [2]: fr-FR [3]: ja-JP The clone array contains the following values: [0]: th-TH [1]: en-US [2]: fr-FR [3]: ja-JP Before changes to the clone: Original: The DateTimeFormatInfo.DateSeparator for ja-JP is /. Clone: The DateTimeFormatInfo.DateSeparator for ja-JP is /. After changes to the clone: Original: The DateTimeFormatInfo.DateSeparator for ja-JP is -. Clone: The DateTimeFormatInfo.DateSeparator for ja-JP is -. */
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.