Q:

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

belongs to collection: C# Basic Programs | array programs

0

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

All Answers

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

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.

 

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

total answers (1)

C# Basic Programs | array programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to produce a third array by appending t... >>
<< C# program to find out the dimensions of an array...