Sponsored Ad

Monday, January 17, 2011

How to Copy an Array into Another Array using C# | Array.Copy()

This sample code is to copy and array into another array using the C# inbuilt method. This method also allow you to specify number of elements to copy in another array. The full code is given below.

Please note that before copying the array you need to create the target array.

How to Copy an Array into Another Array using C# | Array.Copy()

using System;

namespace MyApp
{
    class clsCopyArray
    {
        static void Main(string[] args)
        {
            string[] strArray_Source = { "A", "B", "C", "D", "E", "F", "G", "H", "I"};
            string[] strArray_Target = new string[5];
            Console.WriteLine("Source Array: ");
            foreach (string str in strArray_Source)
                Console.Write(str + " ");

            Console.WriteLine("");

            Console.WriteLine("Target Array: ");
            Array.Copy(strArray_Source, strArray_Target, 5);
            foreach (string str in strArray_Target)
                Console.Write(str + " ");
            Console.ReadLine();
          }         
    }
}

No comments:

Post a Comment

Sponsored Ad

Development Updates