C# Copy Array To Sting
Here are samples of Copying Arrays which are new arrays.
If you want to get a string that comes from the array.
using System;
using System.Collections;
namespace CopyArraySample
{
class Program
{
static void Main(string[] args)
{
int[] pins = { 1, 2, 3, 4 };
Console.WriteLine("pins:");
String s = String.Join(",",pins);
Console.WriteLine(s);
//1.Copy by for
int[] copy1 = new int[pins.Length];
for(int i = 0; i != copy1.Length; i++)
{
copy1[i] = pins[i];
}
Console.WriteLine("copy1:");
String s1 = String.Join(" ", copy1);
Console.WriteLine(s1);
//2.CopyTo()
int[] copy2 = new int[pins.Length];
pins.CopyTo(copy2, 0);
Console.WriteLine("copy2:");
String s2 = String.Join(" ", copy2);
Console.WriteLine(s2);
//3.Copy()
int[] copy3 = new int[pins.Length];
Array.Copy(pins, copy3, pins.Length);
Console.WriteLine("copy3:");
String s3 = String.Join(" ", copy3);
Console.WriteLine(s3);
//4.Clone()
int[] copy4 = (int[])pins.Clone();
Console.WriteLine("copy4:");
String s4 = String.Join(" ", copy4);
Console.WriteLine(s4);
//5.Copy by foreach
ArrayList copy5 = new ArrayList();
foreach (int item in pins)
{
copy5.Add(item);
}
Console.WriteLine("copy5:");
foreach (object o in copy5)
{
Console.Write(o.ToString() + " ");
}
Console.ReadKey();
}
}
}