- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
- Related Articles
- Merge two sorted arrays using C++.
- Merge two sorted arrays into a list using C#
- Merge two sorted arrays in C#
- Merge two sorted arrays in Python using heapq?
- C# program to merge two sorted arrays into one
- What is the AddRange method in C# lists?
- Merge two sorted arrays in Java
- Quickly merging two sorted arrays using std::merge() in C++ STL(cute ho ap)
- How to merge two arrays in JavaScript?
- Merge two arrays keeping original keys in PHP
- Merge two arrays with alternating Values in JavaScript
- JavaScript - Merge two arrays according to id property
- Merge two sorted linked lists using C++.
- Merge two binary Max Heaps using C++.
- How can we merge two JSON arrays in Java?

Advertisements