Copy arbitrary sized array, multiple dimensions and length in C#

Is there a way to create a method, or is there an existing method for Array objects, to copy an arbitrary sized array into a new array.  This isn't that difficult if you know what size and dimensions the original array has, but I am a beginner with c# and I don't know how to copy an array of arbitrary dimensions and size into a new array of the same dimension and length.  I would rather create a method from scratch rather than using some of the built-in methods for Arrays.  It is easy enough to copy into a one-dimsionsal array.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CopyMe
    class Program
        static void Main(string[] args)
            int[,] input = new int[3,2];
            input[0, 0] = 1;
            input[0, 1] = 2;
            input[1, 0] = 3;
            input[1, 1] = 4;
            input[2, 0] = 5;
            input[2, 1] = 6;

            int[] newArray = CopyMe(input);

            int index = 0;
            foreach (int value in newArray)
                Console.WriteLine("Value at index {0} is {1}", index, value);

        public static int[] CopyMe(Array input)
            int[] newArray = new int[input.Length];
            int position = 0;
            foreach (int index in input)
               // Console.Write(index);
                newArray[position] = index;

            Console.WriteLine("Incoming array rank is {0}", input.Rank);
            Console.WriteLine("Incoming array length is {0}", input.Length);
            Console.WriteLine("Hit return to print new array values.");
            return newArray;

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Use Array.Copy (example at the bottom).
sscottiAuthor Commented:
I don't think that that covers the cases where the original array is arbitrary rank.  i.e. [3,2,4,5], [3,3,4,6,7], [4,6,7,8,9,10,11], etc.
Franck GaspozSoftware Architect, Technical ExpertCommented:
if you only want to copy Arrays containing value types and not reference types (for instance objects and not scalar values), you can use the method  Array.Clone
this can works on any arbitrary Array with any dimensions
if you want to write your own method, you will face to the same problem : how to deep clone a reference type ? effectively, the base class 'object' doesn't provide an operator to do that.
In order to deep copy an object of any type, it would be necessary to write a complex method using reflection.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.