Javatpoint Logo
Javatpoint Logo

Buffer.BlockCopy(Array, Int32, Array, Int32, Int32) Method in C#

In this article, we will discuss the Buffer.BlockCopy() method in C# with its syntax, parameters, and examples.

What is the Buffer.BlockCopy() method?

The Buffer.BlockCopy() method in C# provides a strong and fast technique to conduct low-level, memory-oriented operations on arrays. This method is part of the System.namespace. It is widely used in applications that need direct memory manipulation, such as graphics programming, network protocols, or working with data in binary form.

Syntax:

It has the following syntax:

Parameters:

srcs: It is the source buffer.

srcOffsetValue: It is the zero-based byte offset to src.

DST: It is the endpoint of the buffer.

dstOffset: The zero-based bytes offset to dst.

c: The total number of bytes to be copied.

Exception:

  • ArgumentNullException occurs if either src or dst is null.
  • ArgumentException occurs if src or dst are not basic arrays, if src contains fewer bytes than srcOffset plus count, and if the amount of bytes in dst is fewer than dstOffset+count.
  • If srcOffset, dstOffset, or count is less than zero, an ArgumentOutOfRangeException is thrown.

Example 1:

Let us take an example to illustrate the use of Buffer.BlockCopy() method in C#.

Filename: BufferMethod.cs

Output:

Initial Array values:
Array elements in the hexadecimal form:
srcs:000000000000000F  0000000000000010  0000000000000011  0000000000000012 
dests: 0000000000000011  0000000000000012  000000000000000E  0000000000000014 
Individual bytes:
srcs:0F 00 00 00 00 00 00 00 10 00 00 00 0000 00 00 11 00 00 00 00 00 00 00 12 00 00 00 00 00 00 00
dests: 11 00 00 00 00 00 00 00 12 00 00 00 00 00 00 00 0E 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00
Array after operation:
Array element in hexadecimal form:
srcs: 000000000000000F  0000000000000010  0000000000000011  0000000000000012 
dests: 0000000000000011  0000000010000000  000000000000000E  0000000000000014 
Individual bytes:
srcs: 0F 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00 1100 00 00 00 00 00 00 12 00 00 00 00 00 00 00
dests: 11 00 00 00 00 00 00 00 00 00 00 10 00 00 00 00 0E 00 00 00 00 00 00 00 14 00 00 00 0000 00 00

Example 2:

Let us take another example to illustrate the use of Buffer.BlockCopy() method in C#.

FileName: Example2.cs

Output:

Initial Array values:
Array element in hexadecimal form:
src:000000000000000F  0000000000000010  0000000000000011  0000000000000028 dest: 0000000000000011  000000000000004E  0000000000000013  0000000000000028 
Individual bytes:
  src: 0F 00 00 00 00 00 00 00 10 00 00 00 00 00 00 0011 00 00 00 00 00 00 00 28 00 00 00 00 00 00 00
 dest: 11 0000 00 00 00 00 00 4E 00 00 00 00 00 00 00 13 00 00 00 00 0000 00 28 00 00 00 00 00 00 00
src and dst are null:
Exception Thrown: System.ArgumentNullException






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA