how to clone an array in c# code example
Example 1: clone an array c#
float[] newFloats = oldFloats.Clone() as float[];
Example 2: c# array.clone
using System;
using System.Globalization;
public class SamplesArray {
public static void Main() {
CultureInfo ci0 = new CultureInfo( "ar-SA", false );
CultureInfo ci1 = new CultureInfo( "en-US", false );
CultureInfo ci2 = new CultureInfo( "fr-FR", false );
CultureInfo ci3 = new CultureInfo( "ja-JP", false );
CultureInfo[] arrCI = new CultureInfo[] { ci0, ci1, ci2, ci3 };
CultureInfo[] arrCIClone = (CultureInfo[]) arrCI.Clone();
CultureInfo ci4 = new CultureInfo( "th-TH", false );
arrCIClone[0] = ci4;
Console.WriteLine( "The original array contains the following values:" );
PrintIndexAndValues( arrCI );
Console.WriteLine( "The clone array contains the following values:" );
PrintIndexAndValues( arrCIClone );
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 );
arrCIClone[3].DateTimeFormat.DateSeparator = "-";
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 );
}
public static 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 ) );
}
}