- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Merge two arrays using C# AddRange() method
Firstly, set two arrays −
int[] arr1 = { 15, 20, 27, 56 }; int[] arr2 = { 62, 69, 76, 92 };
Now create a new list and use AddRange() method to merge −
var myList = new List<int>(); myList.AddRange(arr1); myList.AddRange(arr2);
After that, convert the merged collection to an array −
int[] arr3 = myList.ToArray()
Let us see the complete code
Example
using System; using System.Collections.Generic; class Demo { static void Main() { int[] arr1 = { 15, 20, 27, 56 }; int[] arr2 = { 62, 69, 76, 92 }; // displaying array1 Console.WriteLine("Array 1..."); foreach(int ele in arr1) { Console.WriteLine(ele); } // displaying array2 Console.WriteLine("Array 2..."); foreach(int ele in arr2) { Console.WriteLine(ele); } var myList = new List<int>(); myList.AddRange(arr1); myList.AddRange(arr2); int[] arr3 = myList.ToArray(); Console.WriteLine("Merged array.."); foreach (int res in arr3) { Console.WriteLine(res); } } }
Output
Array 1... 15 20 27 56 Array 2... 62 69 76 92 Merged array.. 15 20 27 56 62 69 76 92
Advertisements