Q:

C# program to produce a third array by appending two different arrays

0

C# program to produce a third array by appending two different arrays

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

The source code to produce a third array by appending two different arrays in C# is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//Program to produce a third array by 
//appending two different arrays in C#.

using System;

class Demo
{
    static void Main()
    {
        int[] intArr1   = {1,2,3,4,5};
        int[] intArr2   = {6,7,8,9,0};
        int[] intArr3   = new int[10];

        int totalLengthInBytes = 0;

        totalLengthInBytes = intArr1.Length * sizeof(int);
        Buffer.BlockCopy(intArr1, 0, intArr3, 0, totalLengthInBytes);

        totalLengthInBytes = intArr2.Length * sizeof(int);
        Buffer.BlockCopy(intArr2, 0, intArr3, totalLengthInBytes, totalLengthInBytes);

        foreach (int items in intArr3)
        {
            Console.Write(items+ " ");
        }
        Console.WriteLine();
    }
}

Output:

1 2 3 4 5 6 7 8 9 0
Press any key to continue . . .

Explanation:

In the above program, we created three arrays intArray1intArray2, and intArray3. The intArray1 and intArray2 contain 5 items and we occupied space of 10 items for intArray3.

int totalLengthInBytes = 0;

totalLengthInBytes = intArr1.Length * sizeof(int);
Buffer.BlockCopy(intArr1, 0, intArr3, 0, totalLengthInBytes);

totalLengthInBytes = intArr2.Length * sizeof(int);
Buffer.BlockCopy(intArr2, 0, intArr3, totalLengthInBytes, totalLengthInBytes);

In the above code, we copied intArray1 to intArray3 and then appended intArray2 into intArray3 using BlockCopy() method.

foreach (int items in intArr3)
{
    Console.Write(items+ " ");
}

The above code will print all elements of intArray3 on the console screen.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now