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

Here, we are going to learn how to produce a third array by appending two different arrays in C#?
Submitted by Nidhi, on August 22, 2020

Here we will create three integer arrays and then copy array1 to array3 and then append array2 to the array3 then we will get the final result in array3. Here we will use BlockCopy() method to copy one array to another.

Syntax:

void BlockCopy(
    source, 
    offset_source, 
    destination, 
    offse_destination, 
    totalLengthInBytes
    );

Parameter(s):

  • source - Source array to be copied.
  • offset_source - It specifies the offset, from where data to be copied.
  • destination - Destination array.
  • offset_destination - It specifies the offset, from where data will be copied.
  • totalLengthInBytes - It specifies the total bytes to be copied.

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 intArray1, intArray2, 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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.