<p>Is there a built in function in .NET 2.0 that will take two arrays and merge them into one array?</p>

<p>The arrays are both of the same type. I'm getting these arrays from a widely used function within my code base and can't modify the function to return the data in a different format.</p>

<p>I'm looking to avoid writing my own function to accomplish this if possible.</p>

[{'answer_id': 59230, 'author': 'GEOCHET', 'author_id': 5640, 'author_profile': 'https://Stackoverflow.com/users/5640', 'pm_score': 3, 'selected': False, 'text': '<p>I think you can use <a href="http://msdn.microsoft.com/en-us/library/y5s0whfd.aspx" rel="noreferrer">Array.Copy</a> for this. It takes a source index and destination index so you should be able to append the one array to the other. If you need to go more complex than just appending one to the other, this may not be the right tool for you.</p>\n'}, {'answer_id': 59233, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 3, 'selected': False, 'text': '<p>Assuming the destination array has enough space, <a href="https://msdn.microsoft.com/en-us/library/system.array.copy%28v=vs.110%29.aspx" rel="nofollow noreferrer"><code>Array.Copy()</code></a> will work. You might also try using a <code>List&lt;T&gt;</code> and its <a href="https://msdn.microsoft.com/en-us/library/z883w3dc%28v=vs.110%29.aspx" rel="nofollow noreferrer"><code>.AddRange()</code></a> method.</p>\n'}, {'answer_id': 59246, 'author': 'apandit', 'author_id': 6128, 'author_profile': 'https://Stackoverflow.com/users/6128', 'pm_score': 0, 'selected': False, 'text': "<p>I'm assuming you're using your own array types as opposed to the built-in .NET arrays:<br /></p>\n\n<pre><code>public string[] merge(input1, input2)\n{\n    string[] output = new string[input1.length + input2.length];\n    for(int i = 0; i &lt; output.length; i++)\n    {\n        if (i &gt;= input1.length)\n            output[i] = input2[i-input1.length];\n        else\n            output[i] = input1[i];\n    }\n    return output;\n}\n</code></pre>\n\n<p>Another way of doing this would be using the built in ArrayList class.</p>\n\n<pre><code>public ArrayList merge(input1, input2)\n{\n    Arraylist output = new ArrayList();\n    foreach(string val in input1)\n        output.add(val);\n    foreach(string val in input2)\n        output.add(val);\n    return output;\n}\n</code></pre>\n\n<p>Both examples are C#.</p>\n"}, {'answer_id': 59250, 'author': 'Blair Conrad', 'author_id': 1199, 'author_profile': 'https://Stackoverflow.com/users/1199', 'pm_score': 8, 'selected': True, 'text': '<p>If you can manipulate one of the arrays, you can resize it before performing the copy:</p>\n\n<pre><code>T[] array1 = getOneArray();\nT[] array2 = getAnotherArray();\nint array1OriginalLength = array1.Length;\nArray.Resize&lt;T&gt;(ref array1, array1OriginalLength + array2.Length);\nArray.Copy(array2, 0, array1, array1OriginalLength, array2.Length);\n</code></pre>\n\n<p>Otherwise, you can make a new array</p>\n\n<pre><code>T[] array1 = getOneArray();\nT[] array2 = getAnotherArray();\nT[] newArray = new T[array1.Length + array2.Length];\nArray.Copy(array1, newArray, array1.Length);\nArray.Copy(array2, 0, newArray, array1.Length, array2.Length);\n</code></pre>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/system.array.aspx" rel="noreferrer">More on available Array methods on MSDN</a>.</p>\n'}, {'answer_id': 59253, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 4, 'selected': False, 'text': '<p>First, make sure you ask yourself the question "Should I really be using an Array here"?</p>\n\n<p>Unless you\'re building something where speed is of the utmost importance, a typed List, like <code>List&lt;int&gt;</code> is probably the way to go. The only time I ever use arrays are for byte arrays when sending stuff over the network. Other than that, I never touch them.</p>\n'}, {'answer_id': 59262, 'author': 'OwenP', 'author_id': 2547, 'author_profile': 'https://Stackoverflow.com/users/2547', 'pm_score': 9, 'selected': False, 'text': '<p>In C# 3.0 you can use LINQ\'s <a href="https://msdn.microsoft.com/en-us/library/vstudio/bb302894%28v=vs.100%29.aspx" rel="noreferrer">Concat</a> method to accomplish this easily:</p>\n\n<pre><code>int[] front = { 1, 2, 3, 4 };\nint[] back = { 5, 6, 7, 8 };\nint[] combined = front.Concat(back).ToArray();\n</code></pre>\n\n<p>In C# 2.0 you don\'t have such a direct way, but Array.Copy is probably the best solution:</p>\n\n<pre><code>int[] front = { 1, 2, 3, 4 };\nint[] back = { 5, 6, 7, 8 };\n\nint[] combined = new int[front.Length + back.Length];\nArray.Copy(front, combined, front.Length);\nArray.Copy(back, 0, combined, front.Length, back.Length);\n</code></pre>\n\n<p>This could easily be used to implement your own version of <code>Concat</code>.</p>\n'}, {'answer_id': 8770946, 'author': 'namco', 'author_id': 591826, 'author_profile': 'https://Stackoverflow.com/users/591826', 'pm_score': -1, 'selected': False, 'text': '<p>Try this:</p>\n\n<pre><code>ArrayLIst al = new ArrayList();\nal.AddRange(array_1);\nal.AddRange(array_2);\nal.AddRange(array_3);\narray_4 = al.ToArray();\n</code></pre>\n'}, {'answer_id': 15482599, 'author': 'pasx', 'author_id': 683319, 'author_profile': 'https://Stackoverflow.com/users/683319', 'pm_score': 1, 'selected': False, 'text': "<p>Here is a simple example using Array.CopyTo.\nI think that it answers your question and gives an example of CopyTo usage - I am always puzzled when I need to use this function because the help is a bit unclear - the index is the position in the destination array where inserting occurs.</p>\n\n<pre><code>int[] xSrc1 = new int[3] { 0, 1, 2 };\nint[] xSrc2 = new int[5] { 3, 4, 5, 6 , 7 };\n\nint[] xAll = new int[xSrc1.Length + xSrc2.Length];\nxSrc1.CopyTo(xAll, 0);\nxSrc2.CopyTo(xAll, xSrc1.Length);\n</code></pre>\n\n<p>I guess you can't get it much simpler.</p>\n"}, {'answer_id': 15997813, 'author': 'vikasse', 'author_id': 545620, 'author_profile': 'https://Stackoverflow.com/users/545620', 'pm_score': 0, 'selected': False, 'text': '<pre><code>int [] SouceArray1 = new int[] {2,1,3};\nint [] SourceArray2 = new int[] {4,5,6};\nint [] targetArray = new int [SouceArray1.Length + SourceArray2.Length];\nSouceArray1.CopyTo(targetArray,0);\nSourceArray2.CopyTo(targetArray,SouceArray1.Length) ; \nforeach (int i in targetArray) Console.WriteLine(i + " ");  \n</code></pre>\n\n<p>Using the above code two Arrays can be easily merged.</p>\n'}, {'answer_id': 20531421, 'author': 'Lorenz Lo Sauer', 'author_id': 901946, 'author_profile': 'https://Stackoverflow.com/users/901946', 'pm_score': 2, 'selected': False, 'text': '<p>Personally, I prefer my own Language Extensions, which I add or remove at will for rapid prototyping.</p>\n\n<p>Following is an example for strings.</p>\n\n<pre><code>//resides in IEnumerableStringExtensions.cs\npublic static class IEnumerableStringExtensions\n{\n   public static IEnumerable&lt;string&gt; Append(this string[] arrayInitial, string[] arrayToAppend)\n   {\n       string[] ret = new string[arrayInitial.Length + arrayToAppend.Length];\n       arrayInitial.CopyTo(ret, 0);\n       arrayToAppend.CopyTo(ret, arrayInitial.Length);\n\n       return ret;\n   }\n}\n</code></pre>\n\n<p>It is much faster than LINQ and Concat. Faster still, is using a custom <code>IEnumerable</code> Type-wrapper which stores references/pointers of passed arrays and allows looping over the entire collection as if it were a normal array. (Useful in HPC, Graphics Processing, Graphics render...)</p>\n\n<p><strong>Your Code:</strong></p>\n\n<pre><code>var someStringArray = new[]{"a", "b", "c"};\nvar someStringArray2 = new[]{"d", "e", "f"};\nsomeStringArray.Append(someStringArray2 ); //contains a,b,c,d,e,f\n</code></pre>\n\n<p>For the entire code and a generics version see: <a href="https://gist.github.com/lsauer/7919764" rel="nofollow">https://gist.github.com/lsauer/7919764</a></p>\n\n<p><strong>Note:</strong> This returns an unextended IEnumerable object. To return an extended object is a bit slower.</p>\n\n<p><em>I compiled such extensions since 2002, with a lot of credits going to helpful people on CodeProject and \'Stackoverflow\'. I will release these shortly and put the link up here.</em></p>\n'}, {'answer_id': 23822750, 'author': 'Simon B.', 'author_id': 3667854, 'author_profile': 'https://Stackoverflow.com/users/3667854', 'pm_score': 7, 'selected': False, 'text': '<p>Use <a href="http://en.wikipedia.org/wiki/Language_Integrated_Query" rel="noreferrer">LINQ</a>:</p>\n\n<pre><code>var arr1 = new[] { 1, 2, 3, 4, 5 };\nvar arr2 = new[] { 6, 7, 8, 9, 0 };\nvar arr = arr1.Union(arr2).ToArray();\n</code></pre>\n\n<p>Keep in mind, this will remove duplicates. If you want to keep duplicates, use Concat. </p>\n'}, {'answer_id': 24958986, 'author': 'Angelo Ortega', 'author_id': 3877523, 'author_profile': 'https://Stackoverflow.com/users/3877523', 'pm_score': 4, 'selected': False, 'text': '<p>Easier would just be using <a href="http://en.wikipedia.org/wiki/Language_Integrated_Query" rel="noreferrer">LINQ</a>:</p>\n\n<pre><code>var array = new string[] { "test" }.ToList();\nvar array1 = new string[] { "test" }.ToList();\narray.AddRange(array1);\nvar result = array.ToArray();\n</code></pre>\n\n<p>First convert the arrays to lists and merge them... After that just convert the list back to an array :)</p>\n'}, {'answer_id': 27306348, 'author': 'Lukas', 'author_id': 593388, 'author_profile': 'https://Stackoverflow.com/users/593388', 'pm_score': 2, 'selected': False, 'text': '<p>In case someone else is looking for how to merge two image byte arrays:</p>\n\n<pre><code>        private void LoadImage()\n        {\n            string src = string.empty;\n            byte[] mergedImageData = new byte[0];\n\n            mergedImageData = MergeTwoImageByteArrays(watermarkByteArray, backgroundImageByteArray);\n            src = "data:image/png;base64," + Convert.ToBase64String(mergedImageData);\n            MyImage.ImageUrl = src;\n        }\n\n        private byte[] MergeTwoImageByteArrays(byte[] imageBytes, byte[] imageBaseBytes)\n        {\n            byte[] mergedImageData = new byte[0];\n            using (var msBase = new MemoryStream(imageBaseBytes))\n            {\n                System.Drawing.Image imgBase = System.Drawing.Image.FromStream(msBase);\n                Graphics gBase = Graphics.FromImage(imgBase);\n                using (var msInfo = new MemoryStream(imageBytes))\n                {\n                    System.Drawing.Image imgInfo = System.Drawing.Image.FromStream(msInfo);\n                    Graphics gInfo = Graphics.FromImage(imgInfo);\n                    gBase.DrawImage(imgInfo, new Point(0, 0));\n                    //imgBase.Save(Server.MapPath("_____testImg.png"), ImageFormat.Png);\n                    MemoryStream mergedImageStream = new MemoryStream();\n                    imgBase.Save(mergedImageStream, ImageFormat.Png);\n                    mergedImageData = mergedImageStream.ToArray();\n                    mergedImageStream.Close();\n                }\n            }\n            return mergedImageData;\n        }\n</code></pre>\n'}, {'answer_id': 29591722, 'author': 'Rajkumar M', 'author_id': 4779984, 'author_profile': 'https://Stackoverflow.com/users/4779984', 'pm_score': -1, 'selected': False, 'text': '<p>This code will work for all cases:</p>\n\n<pre><code>int[] a1 ={3,4,5,6};\nint[] a2 = {4,7,9};\nint i = a1.Length-1;\nint j = a2.Length-1;\nint resultIndex=  i+j+1;\nArray.Resize(ref a2, a1.Length +a2.Length);\nwhile(resultIndex &gt;=0)\n{\n    if(i != 0 &amp;&amp; j !=0)\n    {\n        if(a1[i] &gt; a2[j])\n        {\n            a2[resultIndex--] = a[i--];\n        }\n        else\n        {\n            a2[resultIndex--] = a[j--];\n        }\n    }\n    else if(i&gt;=0 &amp;&amp; j&lt;=0)\n    { \n        a2[resultIndex--] = a[i--];\n    }\n    else if(j&gt;=0 &amp;&amp; i &lt;=0)\n    {\n       a2[resultIndex--] = a[j--];\n    }\n}\n</code></pre>\n'}, {'answer_id': 35873378, 'author': 'John Reilly', 'author_id': 761388, 'author_profile': 'https://Stackoverflow.com/users/761388', 'pm_score': 3, 'selected': False, 'text': '<p>Everyone has already had their say but I think this more readable than the "use as Extension method" approach:</p>\n\n<pre><code>var arr1 = new[] { 1, 2, 3, 4, 5 };\nvar arr2 = new[] { 6, 7, 8, 9, 0 };\nvar arr = Queryable.Concat(arr1, arr2).ToArray();\n</code></pre>\n\n<p>However it can only be used when bringing together 2 arrays.</p>\n'}, {'answer_id': 42333029, 'author': 'Solomon Rutzky', 'author_id': 577765, 'author_profile': 'https://Stackoverflow.com/users/577765', 'pm_score': 2, 'selected': False, 'text': '<p>Just to have it noted as an option: if the arrays you are working with are of a primitive type &ndash; Boolean (bool), Char, SByte, Byte, Int16 (short), UInt16, Int32 (int), UInt32, Int64 (long), UInt64, IntPtr, UIntPtr, Single, or Double &ndash; then you could (or should?) try using <a href="https://msdn.microsoft.com/en-us/library/system.buffer.blockcopy.aspx" rel="nofollow noreferrer">Buffer.BlockCopy</a>. According to the MSDN page for the <a href="https://msdn.microsoft.com/en-us/library/system.buffer.aspx" rel="nofollow noreferrer">Buffer</a> class:</p>\n\n<blockquote>\n  <p>This class provides better performance for manipulating primitive types than similar methods in the <a href="https://msdn.microsoft.com/en-us/library/system.array.aspx" rel="nofollow noreferrer">System.Array</a> class.</p>\n</blockquote>\n\n<p>Using the C# 2.0 example from @OwenP\'s <a href="https://stackoverflow.com/a/59262/577765">answer</a> as a starting point, it would work as follows:</p>\n\n<pre class="lang-cs prettyprint-override"><code>int[] front = { 1, 2, 3, 4 };\nint[] back = { 5, 6, 7, 8 };\n\nint[] combined = new int[front.Length + back.Length];\nBuffer.BlockCopy(front, 0, combined, 0, front.Length);\nBuffer.BlockCopy(back, 0, combined, front.Length, back.Length);\n</code></pre>\n\n<p>There is barely any difference in syntax between <code>Buffer.BlockCopy</code> and the <code>Array.Copy</code> that @OwenP used, but this should be faster (even if only slightly).</p>\n'}, {'answer_id': 43250371, 'author': 'Smith', 'author_id': 362461, 'author_profile': 'https://Stackoverflow.com/users/362461', 'pm_score': 6, 'selected': False, 'text': "<p>If you don't want to remove duplicates, then try this</p>\n\n<p>Use LINQ:</p>\n\n<pre><code>var arr1 = new[] { 1, 2, 3, 4, 5 };\nvar arr2 = new[] { 6, 7, 8, 9, 0 };\nvar arr = arr1.Concat(arr2).ToArray();\n</code></pre>\n"}, {'answer_id': 46849562, 'author': 'Lord Darth Vader', 'author_id': 2527116, 'author_profile': 'https://Stackoverflow.com/users/2527116', 'pm_score': 0, 'selected': False, 'text': '<p>Created and extension method to handle null</p>\n\n<pre><code>public static class IEnumerableExtenions\n{\n    public static IEnumerable&lt;T&gt; UnionIfNotNull&lt;T&gt;(this IEnumerable&lt;T&gt; list1, IEnumerable&lt;T&gt; list2)\n    {\n        if (list1 != null &amp;&amp; list2 != null)\n            return list1.Union(list2);\n        else if (list1 != null)\n            return list1;\n        else if (list2 != null)\n            return list2;\n        else return null;\n    }\n}\n</code></pre>\n'}, {'answer_id': 49230902, 'author': 'Simon_Weaver', 'author_id': 16940, 'author_profile': 'https://Stackoverflow.com/users/16940', 'pm_score': 2, 'selected': False, 'text': '<p>I needed a solution to combine an unknown number of arrays.</p>\n\n<p>Surprised nobody else provided a solution using <code>SelectMany</code> with <code>params</code>. </p>\n\n<pre><code> private static T[] Combine&lt;T&gt;(params IEnumerable&lt;T&gt;[] items) =&gt;\n                    items.SelectMany(i =&gt; i).Distinct().ToArray();\n</code></pre>\n\n<p>If you don\'t want distinct items just remove distinct.</p>\n\n<pre><code> public string[] Reds = new [] { "Red", "Crimson", "TrafficLightRed" };\n public string[] Greens = new [] { "Green", "LimeGreen" };\n public string[] Blues = new [] { "Blue", "SkyBlue", "Navy" };\n\n public string[] Colors = Combine(Reds, Greens, Blues);\n</code></pre>\n\n<p>Note: There is definitely no guarantee of ordering when using distinct.</p>\n'}, {'answer_id': 55578088, 'author': 'cj.burrow', 'author_id': 6580072, 'author_profile': 'https://Stackoverflow.com/users/6580072', 'pm_score': 3, 'selected': False, 'text': '<p>This is what I came up with. Works for a variable number of arrays. </p>\n\n<pre><code>public static T[] ConcatArrays&lt;T&gt;(params T[][] args)\n    {\n        if (args == null)\n            throw new ArgumentNullException();\n\n        var offset = 0;\n        var newLength = args.Sum(arr =&gt; arr.Length); \n        var newArray = new T[newLength];\n\n        foreach (var arr in args)\n        {\n            Buffer.BlockCopy(arr, 0, newArray, offset, arr.Length);\n            offset += arr.Length;\n        }\n\n        return newArray;\n    }\n</code></pre>\n\n<p>...   </p>\n\n<pre><code>var header = new byte[] { 0, 1, 2};\nvar data = new byte[] { 3, 4, 5, 6 };\nvar checksum = new byte[] {7, 0};\nvar newArray = ConcatArrays(header, data, checksum);\n//output byte[9] { 0, 1, 2, 3, 4, 5, 6, 7, 0 }\n</code></pre>\n'}, {'answer_id': 59576666, 'author': 'schoetbi', 'author_id': 108238, 'author_profile': 'https://Stackoverflow.com/users/108238', 'pm_score': 2, 'selected': False, 'text': '<p>If you have the source arrays in an array itself you can use <a href="https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.selectmany?view=netframework-4.8" rel="nofollow noreferrer">SelectMany</a>:</p>\n\n<pre><code>var arrays = new[]{new[]{1, 2, 3}, new[]{4, 5, 6}};\nvar combined = arrays.SelectMany(a =&gt; a).ToArray();\nforeach (var v in combined) Console.WriteLine(v);   \n</code></pre>\n\n<p>gives</p>\n\n<pre><code>1\n2\n3\n4\n5\n6\n</code></pre>\n\n<p>Probably this is not the fastest method but might fit depending on usecase.</p>\n'}, {'answer_id': 66751270, 'author': 'Dmitry Shashurov', 'author_id': 4309436, 'author_profile': 'https://Stackoverflow.com/users/4309436', 'pm_score': -1, 'selected': False, 'text': '<p>Simple code to join multiple arrays:</p>\n<pre><code>string[] arr1 = ...\nstring[] arr2 = ...\nstring[] arr3 = ...    \nList&lt;string&gt; arr = new List&lt;string&gt;(arr1.Length + arr2.Length + arr3.Length);\narr.AddRange(arr1);\narr.AddRange(arr2);\narr.AddRange(arr3);\nstring[] result = arr.ToArray();\n</code></pre>\n'}, {'answer_id': 67243561, 'author': 'Mondonno', 'author_id': 11824362, 'author_profile': 'https://Stackoverflow.com/users/11824362', 'pm_score': -1, 'selected': False, 'text': '<p>This is another way to do this :)</p>\n<pre class="lang-cs prettyprint-override"><code>public static void ArrayPush&lt;T&gt;(ref T[] table, object value)\n{\n    Array.Resize(ref table, table.Length + 1); // Resizing the array for the cloned length (+-) (+1)\n    table.SetValue(value, table.Length - 1); // Setting the value for the new element\n}\n\npublic static void MergeArrays&lt;T&gt;(ref T[] tableOne, T[] tableTwo) {\n    foreach(var element in tableTwo) {\n        ArrayPush(ref tableOne, element);\n    }\n}\n</code></pre>\n<p>Here is the <a href="https://dotnetfiddle.net/5B5Oyj" rel="nofollow noreferrer">snippet/example</a></p>\n'}, {'answer_id': 68940225, 'author': 'Harjeet Singh', 'author_id': 4960384, 'author_profile': 'https://Stackoverflow.com/users/4960384', 'pm_score': 0, 'selected': False, 'text': '<pre><code>string[] names1 = new string[] { &quot;Ava&quot;, &quot;Emma&quot;, &quot;Olivia&quot; };\nstring[] names2 = new string[] { &quot;Olivia&quot;, &quot;Sophia&quot;, &quot;Emma&quot; };\nList&lt;string&gt; arr = new List&lt;string&gt;(names1.Length + names2.Length);\narr.AddRange(names1);\narr.AddRange(names2);\nstring[] result = arr.Distinct().ToArray();\nforeach(string str in result)\n{\n    Console.WriteLine(str.ToString());\n}\n\nConsole.ReadLine();\n</code></pre>\n'}, {'answer_id': 74140785, 'author': 'ArrayFormula', 'author_id': 13501781, 'author_profile': 'https://Stackoverflow.com/users/13501781', 'pm_score': 0, 'selected': False, 'text': "<p>I wanted to find an approach without using any libraries or functionality beyond arrays themselves.</p>\n<p>The first two examples are mostly for reading the logic from scratch, but I also wonder if there could be performance variations depending on the sitaution.</p>\n<p>The third example is the most practical choice.</p>\n<pre><code>// Two for-loops\nprivate static int[] MergedArrays_1(int[] a, int[] b)\n{\n    int[] result = new int[a.Length + b.Length];\n    for (int i = 0; i &lt; a.Length; i++)\n    {\n        result[i] = a[i];\n    }\n    for (int i = a.Length; i &lt; result.Length; i++)\n    {\n        result[i] = b[i - a.Length];\n    }\n    return result;\n}\n\n// One for-loop\nprivate static int[] MergedArrays_2(int[] a, int[] b)\n{\n    int[] results = new int[a.Length + b.Length];\n    for (int i = 0; i &lt; results.Length; i++)\n    {\n        results[i] = (i &lt; a.Length) ? a[i] : b[i - a.Length];\n    }\n    return results;\n}\n\n// Array Method\nprivate static int[] MergedArrays_3(int[] a, int[] b)\n{\n    int[] results = new int[a.Length + b.Length];\n    a.CopyTo(results, 0);\n    b.CopyTo(results, a.Length);\n    return results;\n}\n</code></pre>\n<p>Lastly, I made a fourth example, that can merge multiple arrays, using the params keyword.</p>\n<pre><code>int[] result = MultipleMergedArrays(arrayOne, arrayTwo, arrayThree);\n</code></pre>\n<pre><code>private static int[] MultipleMergedArrays(params int[][] a)\n{\n    // Get Length\n    int resultsLength = 0;\n    for (int row = 0; row &lt; a.GetLength(0); row++)\n    {\n        resultsLength += a.Length;\n    }\n\n    // Initialize\n    int[] results = new int[resultsLength];\n\n    // Add Items\n    int index = 0;\n    for (int row = 0; row &lt; a.GetLength(0); row++)\n    {\n        a[row].CopyTo(results, index);\n        index += a[row].Length;\n    }\n    return results;\n}\n</code></pre>\n<p>The way it works when using params, is that the single-dimension arrays are passed into a jagged array.</p>\n<p>GetLength(0) returns the number of arrays contained within the jagged array.</p>\n<p>The code first counts the Length of all the arrays, then it initializes a new array based on that size, and starts adding entire arrays into the new results array by using the CopyTo() method, while adding the Length of each added array to an index counter.</p>\n<p>PS:  Some times it is necessary to remove empty items, or certain items, from arrays when merging.</p>\n<pre><code>private static int[] RemoveEmpty(int[] array)\n{\n    int count = 0;\n    for (int i = 0; i &lt; array.Length; i++)\n    {\n        if (array[i] == 0) count++;\n    }\n\n    int[] result = new int[array.Length - count];\n\n    count = 0;\n    for (int i = 0; i &lt; array.Length; i++)\n    {\n        if (array[i] == 0) continue;\n        result[count] = array[i];\n        count++;\n    }\n\n    return result;\n}\n</code></pre>\n<p>This function can be combined with the ones above.</p>\n<p>It takes an array, counts the number of items that match zero. And creates a new array of the proper size. Then the counter is recycled and used as an index, for where to put place the input array's values into the new and smaller result array.\nWhen an item matches zero, it skips the rest of the code in that round of the loop, and continues with the next round, without incrementing the integer counter.</p>\n"}]
['https://Stackoverflow.com/questions/59217', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3058/']