TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# Combine Arrays: List, Array.Copy and Buffer.BlockCopy

Combine two one-dimensional arrays into a single array. Use List, AddRange, Array.Copy and Buffer.BlockCopy.
Combine arrays. Two arrays can be merged. We have two arrays with the same types of elements. For further processing in your program, we must combine these 2 arrays.Array
This allows us to use both parts of the data in a single collection. Several approaches are possible. They all have benefits and negatives.
AddRange. This code is unlikely to fail due to off-by-one programming errors. When you have 2 arrays, you can use several ways to combine them.

Here: We use the List type, and call the AddRange() and ToArray methods to get the combined array.

AddRange, InsertRangeToArray

List: The program uses the List type to provide a level of abstraction over the actual copying to the new array.

Tip: The AddRange method invocations will actually loop through the array1 and array2 elements and copy them into new memory.

List
C# program that combines two arrays using System; using System.Collections.Generic; class Program { static void Main() { // ::: Declare two integer arrays with five elements each int[] array1 = { 1, 2, 3, 4, 5 }; int[] array2 = { 6, 7, 8, 9, 10 }; // ::: Loop through the two arrays and print them foreach (int element in array1) { Console.WriteLine(element); } foreach (int element in array2) { Console.WriteLine(element); } // ::: Create new List of integers and call AddRange twice var list = new List<int>(); list.AddRange(array1); list.AddRange(array2); // ::: Call ToArray to convert List to array int[] array3 = list.ToArray(); // ::: Loop through array elements of combined array and print them foreach (int element in array3) { Console.WriteLine(element); } } } Output 1 (array1 start) (array3 start) 2 3 4 5 6 (array2 start) 7 8 9 10
Array.Copy. This example program shows how to use Array.Copy to combine arrays. This is more efficient than the List approach. It only requires a new array.Array.Copy

Note: This pattern of code is the same as that for the Buffer.BlockCopy method shown next: it just uses element counts, not byte counts.

C# program that uses Array.Copy using System; class Program { static void Main() { int[] values1 = { 4, 4, 4 }; int[] values2 = { 5, 5 }; int[] all = new int[values1.Length + values2.Length]; Array.Copy(values1, all, values1.Length); Array.Copy(values2, 0, all, values1.Length, values2.Length); foreach (int value in all) { Console.WriteLine(value); } } } Output 4 4 4 5 5
BlockCopy. Next we use Buffer.BlockCopy to merge two int arrays. This method acts upon bytes, not elements (which are four bytes here).

Important: We must multiply the element counts by the sizeof(int) to get correct units.

Buffer

Here: We merge two 3-element int arrays. We BlockCopy the first into the "final" array, and then BlockCopy the second.

Tip: This version would be the fastest one according to my previous benchmarks of BlockCopy.

C# program that uses BlockCopy using System; class Program { static void Main() { // ... Two input arrays. int[] array = { 1, 2, 3 }; int[] array2 = { 4, 5, 6 }; // ... Destination array. int[] final = new int[array.Length + array2.Length]; // ... Copy first array. Buffer.BlockCopy(array, 0, final, 0, array.Length * sizeof(int)); // ... Copy second. // Note the starting offset. Buffer.BlockCopy(array2, 0, final, array.Length * sizeof(int), array2.Length * sizeof(int)); // ... Display. foreach (int value in final) { Console.WriteLine(value); } } } Output 1 2 3 4 5 6
AddRange, internals. Here we discuss the internal implementation of the AddRange() method. The AddRange method internally calls InsertRange.

And: It calls the fast Array.Copy method to do a bitwise copy. If you call Array.Copy manually, you could improve performance.

A summary. With AddRange on the List we can combine arrays. This method will also work with more than 2 arrays. The arrays must all have the same type of elements.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf