C# Program to Merge Two Arrays into Third Array

This is a C# Program to Get 2 arrays as input and produce a 3rd array by appending one to other.

Problem Description

This C# Program Gets 2 Arrays as Input and Produce a 3rd Array by Appending one to other.

Problem Solution

Here Buffer.BlockCopy is used to merge two int arrays. This method acts upon bytes, not elements.

Program/Source Code

Here is source code of the C# Program to Get 2 Arrays as Input and Produce a 3rd Array by Appending one to other. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Get 2 Arrays as Input and Produce a 3rd Array by Appending one to 
 * other
 */
using System;
 
class Program
{
    static void Main()
    {
        int[] array1 = new int[5];
        int[] array2 = new int[5];
        int[] array3 = new int[array1.Length + array2.Length];
        Console.WriteLine("Enter Any 5 Elements for the First Array :");
        for (int i = 0; i < 5; i++)
        {
            array1[i] = int.Parse(Console.ReadLine());
        }
        Console.WriteLine("Enter Any 5 Elements for the Second Array :");
        for (int i = 0; i < 5; i++)
        {
            array2[i] = int.Parse(Console.ReadLine());
        }
        Buffer.BlockCopy(array1,0,array3,0,array1.Length * sizeof(int));
        Buffer.BlockCopy(array2,0,array3,array1.Length * sizeof(int),
                         array2.Length * sizeof(int));
        Console.WriteLine("Elements in the Third Array After Appending" +
                          " First and Second Arrays :");
        foreach (int value in array3)
        {
            Console.WriteLine(value);
        }
        Console.ReadLine();
    }
}
Program Explanation

This C# program is used to get 2 arrays as input and produce a 3rd array by appending one to other. Using for loop we are entering the coefficient element values for the array1 and array2 variables.

advertisement
advertisement

The Buffer.BlockCopy() function is used to merge two int arrays. This method acts upon bytes, not elements. Using foreach loop print the elements in third array after appending with first and second arrays.

Here the foreach statement is used to iterate through the collection to get the information that you want, but cannot be used to add or remove items from the source collection to avoid unpredictable side effects.

Runtime Test Cases
 
Enter Any 5 Elements for the First Array :
1
2
3
4
5
Enter Any 5 Elements for the Second Array :
6
7
8
9
10
Elements in the Third Array After Appending First and Second Arrays :
1
2
3
4
5
6
7
8
9
10

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.