Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to use Array.CopyTo() method to copy all elements of 1D array to other 1D array starting at index N in C#

2 Answers

0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Array sourceArray = Array.CreateInstance(typeof(String), 3);

            sourceArray.SetValue("aaa", 0);
            sourceArray.SetValue("bbb", 1);
            sourceArray.SetValue("ccc", 2);

            Array targetArray = Array.CreateInstance(typeof(String), 3);

            sourceArray.CopyTo(targetArray, 0);

            PrintArray(targetArray);
        }
        public static void PrintArray(Array arr)
        {
            for (int i = arr.GetLowerBound(0); i <= arr.GetUpperBound(0); i++)
                Console.WriteLine("arr[{0}] = {1}", i, arr.GetValue(i));
        }
    }
}


/*
run:
 
arr[0] = aaa
arr[1] = bbb
arr[2] = ccc

*/

 



answered Apr 15, 2016 by avibootz
edited Apr 15, 2016 by avibootz
0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Array sourceArray = Array.CreateInstance(typeof(String), 3);

            sourceArray.SetValue("aaa", 0);
            sourceArray.SetValue("bbb", 1);
            sourceArray.SetValue("ccc", 2);

            Array targetArray = Array.CreateInstance(typeof(String), 4);
            targetArray.SetValue("yyy", 0);
            targetArray.SetValue("zzz", 1);

            sourceArray.CopyTo(targetArray, 1);

            PrintArray(targetArray);
        }
        public static void PrintArray(Array arr)
        {
            for (int i = arr.GetLowerBound(0); i <= arr.GetUpperBound(0); i++)
                Console.WriteLine("arr[{0}] = {1}", i, arr.GetValue(i));
        }
    }
}


/*
run:
 
arr[0] = yyy
arr[1] = aaa
arr[2] = bbb
arr[3] = ccc

*/

 



answered Apr 15, 2016 by avibootz
...