کپی کردن یک آرایه در آرایه دیگر با متد CopyTo از کلاس Array
با استفاده از کد زیر می توانید یک آرایه را از یک موقعیت خاص در یک آرایه دیگر کپی کنید :
using System; public class CopyToArray { public static void Main() { object[] objects1 = {"one", "two", "three"}; object[] objects2 = {0, 1, 2, 3, 4, 5}; Console.Write ("objects1 array elements: "); foreach(object o in objects1) { Console.Write ("{0} ", o); } Console.Write ("nobjects2 array elements: "); foreach (object o in objects2) { Console.Write ("{0} ", o); } objects1.CopyTo (objects2, 1); Console.Write ("nobjects2 array elements: "); foreach (object o in objects2) { Console.Write ("{0} ", o); } Console.WriteLine(); } }
خروجی کد بالا به صورت زیر است :
objects1 array elements: one two three objects2 array elements: 0 1 2 3 4 5 objects2 array elements: 0 one two three 4 5