C# | Buffer.BlockCopy(Array, Int32, Array, Int32, Int32) Method
This method is used to copy a specified number of bytes from a source array starting at a particular offset to a destination array starting at a particular offset.
Syntax:
public static void BlockCopy (Array src, int srcOffset, Array dst, int dstOffset, int count);
Parameters:
src: It is the source buffer.
srcOffset: It is the zero-based byte offset into src.
dst: It is the destination buffer.
dstOffset: The zero-based byte offset into dst.
count: The number of bytes to copy.
Exceptions:
- ArgumentNullException: if src or dst is null.
- ArgumentException: if src or dst is not an array of primitives OR the number of bytes in src is less than srcOffset plus count. OR the number of bytes in dst is less than dstOffset plus count.
- ArgumentOutOfRangeException: if srcOffset, dstOffset, or count is less than 0.
Below programs illustrate the use of Buffer.BlockCopy(Array, Int32, Array, Int32, Int32) Method:
Example 1:
// C# program to demonstrate // Buffer.BlockCopy(Array, Int32, // Array, Int32, Int32) Method using System; class GFG { // Main Method public static void Main() { try { // Declaring src and dest array long[] src = {15, 16, 17, 18 }; long[] dest = {17, 18, 19, 20 }; Console.WriteLine("Initial Array values:"); Console.WriteLine(); // Display hexadecimal values Console.WriteLine("Array element in hexadecimal form:"); displayhexvalue(src, "src"); displayhexvalue(dest, "dest"); // display Individual byte Console.WriteLine("Individual bytes:"); displaybytes(src, "src"); displaybytes(dest, "dest"); // copying the specified number of bytes // using Buffer.BlockCopy() method Buffer.BlockCopy(src, 4, dest, 7, 6); Console.WriteLine(); Console.WriteLine("Array after operation:"); Console.WriteLine(); // display hexadecimal values Console.WriteLine("Array element in hexadecimal form:"); displayhexvalue(src, "src"); displayhexvalue(dest, "dest"); // display hexadecimal value Console.WriteLine("Individual bytes:"); displaybytes(src, "src"); displaybytes(dest, "dest"); } catch (ArgumentNullException e) { Console.Write("Exception Thrown: "); Console.Write("{0}", e.GetType(), e.Message); } catch (ArgumentOutOfRangeException e) { Console.Write("Exception Thrown: "); Console.Write("{0}", e.GetType(), e.Message); } catch (ArgumentException e) { Console.Write("Exception Thrown: "); Console.Write("{0}", e.GetType(), e.Message); } } // Display the individual // array element values // in hexadecimal. public static void displayhexvalue(Array a, string name) { // print the name Console.Write("{0, 5}:", name); // Display value one by one for (int i = 0; i < a.Length; i++) Console.Write(" {0:X16} ", a.GetValue(i)); Console.WriteLine(); } // Display the individual // bytes in the array // in hexadecimal. public static void displaybytes(Array arr, string name) { // print the name Console.Write("{0, 5}:", name); // loop to traverse // every element of the array for (int i = 0; i < arr.Length; i++) { // gettig byte array // converted from long array byte[] bytes = BitConverter.GetBytes((long)arr.GetValue(i)); // display each byte value foreach(byte byteValue in bytes) Console.Write(" {0:X2}", byteValue); } Console.WriteLine(); } } |
Output:
Initial Array values:
Array element in hexadecimal form:
src: 000000000000000F 0000000000000010 0000000000000011 0000000000000012
dest: 0000000000000011 0000000000000012 0000000000000013 0000000000000014
Individual bytes:
src: 0F 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00 11 00 00 00 00 00 00 00 12 00 00 00 00 00 00 00
dest: 11 00 00 00 00 00 00 00 12 00 00 00 00 00 00 00 13 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00Array after operation:
Array element in hexadecimal form:
src: 000000000000000F 0000000000000010 0000000000000011 0000000000000012
dest: 0000000000000011 0000000010000000 0000000000000013 0000000000000014
Individual bytes:
src: 0F 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00 11 00 00 00 00 00 00 00 12 00 00 00 00 00 00 00
dest: 11 00 00 00 00 00 00 00 00 00 00 10 00 00 00 00 13 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00
Example 2: For ArgumentNullException
// C# program to demonstrate // Buffer.BlockCopy(Array, Int32, // Array, Int32, Int32) Method using System; class GFG { // Main Method public static void Main() { try { // Declaring src and dest array long[] src = {15, 16, 17, 18}; long[] dest = {17, 18, 19, 20}; Console.WriteLine("Initial Array values:"); Console.WriteLine(); // Display hexadecimal values Console.WriteLine("Array element in hexadecimal form:"); displayhexvalue(src, "src"); displayhexvalue(dest, "dest"); // display Individual byte Console.WriteLine("Individual bytes:"); displaybytes(src, "src"); displaybytes(dest, "dest"); // copying the specified number of bytes // using Buffer.BlockCopy() method Console.WriteLine("src and dst are null:"); Buffer.BlockCopy(null, 4, null, 7, 6); Console.WriteLine(); Console.WriteLine("Array after operation:"); Console.WriteLine(); // display hexadecimal values Console.WriteLine("Array element in hexadecimal form:"); displayhexvalue(src, "src"); displayhexvalue(dest, "dest"); // display hexadecimal value Console.WriteLine("Individual bytes:"); displaybytes(src, "src"); displaybytes(dest, "dest"); } catch (ArgumentNullException e) { Console.Write("Exception Thrown: "); Console.Write("{0}", e.GetType(), e.Message); } catch (ArgumentOutOfRangeException e) { Console.Write("Exception Thrown: "); Console.Write("{0}", e.GetType(), e.Message); } catch (ArgumentException e) { Console.Write("Exception Thrown: "); Console.Write("{0}", e.GetType(), e.Message); } } // Display the individual array // element values in hexadecimal. public static void displayhexvalue(Array a, string name) { // print the name Console.Write("{0, 5}:", name); // Display value one by one for (int i = 0; i < a.Length; i++) Console.Write(" {0:X16} ", a.GetValue(i)); Console.WriteLine(); } // Display the individual bytes // in the array in hexadecimal. public static void displaybytes(Array arr, string name) { // print the name Console.Write("{0, 5}:", name); // loop to traverse every // element of the array for (int i = 0; i < arr.Length; i++) { // gettig byte array converted // from long array byte[] bytes = BitConverter.GetBytes((long)arr.GetValue(i)); // display each byte value foreach(byte byteValue in bytes) Console.Write(" {0:X2}", byteValue); } Console.WriteLine(); } } |
Output:
Initial Array values:
Array element in hexadecimal form:
src: 000000000000000F 0000000000000010 0000000000000011 0000000000000012
dest: 0000000000000011 0000000000000012 0000000000000013 0000000000000014
Individual bytes:
src: 0F 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00 11 00 00 00 00 00 00 00 12 00 00 00 00 00 00 00
dest: 11 00 00 00 00 00 00 00 12 00 00 00 00 00 00 00 13 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00
src and dst are null:
Exception Thrown: System.ArgumentNullException
Example 3: For ArgumentOutOfRangeException
// C# program to demonstrate // Buffer.BlockCopy(Array, Int32, // Array, Int32, Int32) Method using System; class GFG { // Main Method public static void Main() { try { // Declaring src and dest array long[] src = {15, 16, 17, 18}; long[] dest = {17, 18, 19, 20}; Console.WriteLine("Initial Array values:"); Console.WriteLine(); // Display hexadecimal values Console.WriteLine("Array element in hexadecimal form:"); displayhexvalue(src, "src"); displayhexvalue(dest, "dest"); // display Individual byte Console.WriteLine("Individual bytes:"); displaybytes(src, "src"); displaybytes(dest, "dest"); // copying the specified number of bytes // using Buffer.BlockCopy() method Console.WriteLine("srcoffest and destoffset are less than zero:"); Buffer.BlockCopy(src, -4, dest, -7, 6); Console.WriteLine(); Console.WriteLine("Array after operation:"); Console.WriteLine(); // display hexadecimal values Console.WriteLine("Array element in hexadecimal form:"); displayhexvalue(src, "src"); displayhexvalue(dest, "dest"); // display hexadecimal value Console.WriteLine("Individual bytes:"); displaybytes(src, "src"); displaybytes(dest, "dest"); } catch (ArgumentNullException e) { Console.Write("Exception Thrown: "); Console.Write("{0}", e.GetType(), e.Message); } catch (ArgumentOutOfRangeException e) { Console.Write("Exception Thrown: "); Console.Write("{0}", e.GetType(), e.Message); } catch (ArgumentException e) { Console.Write("Exception Thrown: "); Console.Write("{0}", e.GetType(), e.Message); } } // Display the individual array // element values in hexadecimal. public static void displayhexvalue(Array a, string name) { // print the name Console.Write("{0, 5}:", name); // Display value one by one for (int i = 0; i < a.Length; i++) Console.Write(" {0:X16} ", a.GetValue(i)); Console.WriteLine(); } // Display the individual bytes // in the array in hexadecimal. public static void displaybytes(Array arr, string name) { // print the name Console.Write("{0, 5}:", name); // loop to traverse every // element of the array for (int i = 0; i < arr.Length; i++) { // gettig byte array converted // from long array byte[] bytes = BitConverter.GetBytes((long)arr.GetValue(i)); // display each byte value foreach(byte byteValue in bytes) Console.Write(" {0:X2}", byteValue); } Console.WriteLine(); } } |
Output:
Initial Array values:
Array element in hexadecimal form:
src: 000000000000000F 0000000000000010 0000000000000011 0000000000000012
dest: 0000000000000011 0000000000000012 0000000000000013 0000000000000014
Individual bytes:
src: 0F 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00 11 00 00 00 00 00 00 00 12 00 00 00 00 00 00 00
dest: 11 00 00 00 00 00 00 00 12 00 00 00 00 00 00 00 13 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00
srcoffest and destoffset are less than zero:
Exception Thrown: System.ArgumentOutOfRangeException
Example 4: For ArgumentException
// C# program to demonstrate // Buffer.BlockCopy(Array, Int32, // Array, Int32, Int32) // Method using System; class GFG { // Main Method public static void Main() { try { // Declaring src and dest array long[] src = {15, 16, 17, 18}; long[] dest = {17, 18, 19, 20}; Console.WriteLine("Initial Array values:"); Console.WriteLine(); // Display hexadecimal values Console.WriteLine("Array element in hexadecimal form:"); displayhexvalue(src, "src"); displayhexvalue(dest, "dest"); // display Individual byte Console.WriteLine("Individual bytes:"); displaybytes(src, "src"); displaybytes(dest, "dest"); // copying the specified number of bytes // using Buffer.BlockCopy() method Console.WriteLine("srcoffest and destoffset are less than zero:"); Buffer.BlockCopy(src, 4, dest, 7, 70); Console.WriteLine(); Console.WriteLine("Array after operation:"); Console.WriteLine(); // display hexadecimal values Console.WriteLine("Array element in hexadecimal form:"); displayhexvalue(src, "src"); displayhexvalue(dest, "dest"); // display hexadecimal value Console.WriteLine("Individual bytes:"); displaybytes(src, "src"); displaybytes(dest, "dest"); } catch (ArgumentNullException e) { Console.Write("Exception Thrown: "); Console.Write("{0}", e.GetType(), e.Message); } catch (ArgumentOutOfRangeException e) { Console.Write("Exception Thrown: "); Console.Write("{0}", e.GetType(), e.Message); } catch (ArgumentException e) { Console.Write("Exception Thrown: "); Console.Write("{0}", e.GetType(), e.Message); } } // Display the individual array // element values in hexadecimal. public static void displayhexvalue(Array a, string name) { // print the name Console.Write("{0, 5}:", name); // Display value one by one for (int i = 0; i < a.Length; i++) Console.Write(" {0:X16} ", a.GetValue(i)); Console.WriteLine(); } // Display the individual bytes // in the array in hexadecimal. public static void displaybytes(Array arr, string name) { // print the name Console.Write("{0, 5}:", name); // loop to traverse every // element of the array for (int i = 0; i < arr.Length; i++) { // getting byte array // converted from long array byte[] bytes = BitConverter.GetBytes((long)arr.GetValue(i)); // display each byte value foreach(byte byteValue in bytes) Console.Write(" {0:X2}", byteValue); } Console.WriteLine(); } } |
Output:
Initial Array values:
Array element in hexadecimal form:
src: 000000000000000F 0000000000000010 0000000000000011 0000000000000012
dest: 0000000000000011 0000000000000012 0000000000000013 0000000000000014
Individual bytes:
src: 0F 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00 11 00 00 00 00 00 00 00 12 00 00 00 00 00 00 00
dest: 11 00 00 00 00 00 00 00 12 00 00 00 00 00 00 00 13 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00
srcoffest and destoffset are less than zero:
Exception Thrown: System.ArgumentException
Reference:
Recommended Posts:
- Array.BinarySearch(Array, Int32, Int32, Object) Method with examples in C#
- C# | Array.BinarySearch(Array, Int32, Int32, Object, IComparer) Method
- C# | Int32.ToString Method | Set - 2
- C# | Int32.ToString Method | Set - 1
- Int32.Equals Method in C# with Examples
- Int32.CompareTo Method in C# with Examples
- Int32.GetTypeCode Method in C# with Examples
- Int32.GetHashCode Method in C# with Examples
- C# | Char.ConvertFromUtf32(Int32) Method
- C# | Char.IsHighSurrogate(String, Int32) Method
- C# | Char.IsSurrogate(String, Int32) Method
- Int32.Parse(String) Method in C# with Examples
- C# | Char.IsControl(String, Int32) Method
- C# | Char.IsLowSurrogate(String, Int32) Method
- C# | Char.ConvertToUtf32(String, Int32) Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : shubham_singh



