C# program to demonstrate the example of BlockCopy() method of the array

Here, we are going to learn about the BlockCopy method and its C# implementation.
Submitted by Nidhi, on August 22, 2020

Here we will demonstrate the BlockCopy() method of the array. The BlockCopy() method is used to copy one array to another array.

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 demonstrate the BlockCopy() method in C# is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//Program to demonstrate the BlockCopy() method 
//of the array in C#. 

using System;

class Demo
{
    static void Main()
    {
        int[] source      = {1,2,3,4,5};
        int[] destination = new int[5];
        
        int totalLengthInBytes = source.Length * sizeof(int);
        
        Buffer.BlockCopy(source, 0, destination, 0, totalLengthInBytes);
        
        foreach (int items in destination)
        {
            Console.Write(items+ " ");
        }
        Console.WriteLine();
    }
}

Output:

1 2 3 4 5
Press any key to continue . . .

Explanation:

In the above program, we created two integer arrays source and destination.

int totalLengthInBytes = source.Length * sizeof(int);

In the above code, we find the total number of bytes. Because Length property returns the number of elements in an array and sizeof(int) return the total bytes occupied by an integer and then we multiplied both values and get total length of an array in bytes.






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.