Understanding the System.Buffer Class

Introduction:

Before we try out something with Buffer class lets understand Buffer class and its Members. Yes the first question comes in our mind is how they are different from Array class and how they work when compared to Array class. This article of mine is basically to clear the above questions and understand how Buffer class works. 

The word Buffer itself says that it works on direct Memory. In .Net language it's basically a manipulation of unmanaged memory represented as arrays of bytes. So lets take a example of a program where we will copy one array data into another using Array class as first example and compare it with Buffer class example. 

In System.Array class we have Copy() Member to copy from one array to another.

Lets take an example with a array of 5 elements Myarr1[5] initialized with data 1,2,3,4,5 and another array of 10 elements Myarr2[10] with data 0,0,0,0,0,6,7,8,9,10. 

In arrays the length means number of elements in array. In our example Myarr1 has 5 elements so array length is 5. And for Myarr2 has 10 elements so the array length is 10. 

The Array class has Copy() method which copies one array content into another. It Copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. So Copy() method takes five parameters .They are source array , source index, destination array , destination index, number of elements to copy. 

Before Array.Copy() operation: 

Myarr1[1] Myarr1[2] Myarr1[3]
Myarr3[4] Myarr4[5]
1(data)  2(data) 3(data)  4(data) 5(data)

 

Myarr2[1] Myarr2[1] Myarr2[1] Myarr2[1] Myarr2[1] Myarr2[1] Myarr2[1] Myarr2[1] Myarr2[1] Myarr2[1]
0(data) 0(data) 0(data) 0(data) 0(data) 6(data) 7(data) 8(data) 9(data) 10(data)

After Array.Copy() operation: 

Myarr1[1] Myarr1[2] Myarr1[3] Myarr3[4] Myarr4[5]
1(data) 2(data) 3(data) 4(data) 5(data

 

Myarr2[1] Myarr2[1] Myarr2[1] Myarr2[1] Myarr2[1] Myarr2[1] Myarr2[1] Myarr2[1] Myarr2[1] Myarr2[1]
1(data) 2(data) 3(data) 4(data) 5(data) 6(data) 7(data) 8(data) 9(data) 10(data)
 

Here is the program for using Array.Copy(): 

namespace ConsoleApplication1
{
class Array1
{
static void Main(string[] args)
{
int[] myarr1 = new int[5] {1,2,3,4,5};
int[] myarr2=new int[10] {0,0,0,0,0,6,7,8,9,10};
Console.Write("Before Array copy operation\n");
Console.Write("Myarr1 and Byte Length{0}\n",myarr1.Length);
foreach(int i in myarr1)
Console.Write("{0} \t",i);
Console.WriteLine("\nMyarr2 and Byte Length:{0} \n",myarr2.Length);
foreach(int i in myarr2)
Console.Write("{0} \t",i);
Array.Copy(myarr1,0,myarr2,0,5);
Console.Write("After Array copy operation\n");
Console.Write("Myarr1 :\n");
foreach(int i in myarr1)
Console.Write("{0} \t",i);
Console.WriteLine("\nMyarr2: \n");
foreach(int i in myarr2)
Console.Write("{0} \t",i);
Console.ReadLine();
}
}
}

Output:

Before Array Copy operation

Myarr1 and Byte Length :5

1       2       3       4       5

Myarr2 and Byte Length:10

0       0       0       0       0       6       7       8       9       10

After Array Copy operation

Myarr1 :

1       2       3       4       5

Myarr2:

1       2       3       4       5       6       7       8       9       10

Now we will see same thing can be done using System.Buffer class using BlockCopy() method. But its not index to index copy in Buffer class. It's from offset to offset. So it copies a specified number of bytes from a source array starting at a particular offset to a destination array starting at a particular offset. Since we have taken integer arrays as example and we know int occupies 4bytes. So offset values will be addition of 4bytes from starting offset value.

In System.Buffer class we have BlockCopy() Member to copy from one array to another.Lets take an example with a array of 5 elements Myarr1[5] initialized with data 1,2,3,4,5 and another array of 10 elements Myarr2[10] with data 0,0,0,0,0,6,7,8,9,10. 

In Buffer class the length means number of byte length in array. In our example Myarr1 has 5 elements so byte length is 5 (elements) x 4 (bytes of int)= 20 bytes. And for Myarr2 has 10 elements so the byte length is 10 (elements) x 4 (bytes of int) = 40 bytes. 

You can get the byte length of array by using Buffer.ByteLength(Myarr1). 

The Buffer class has BlockCopy() method which copies one array content into another. It Copies a range of elements from an Array starting at the specified source start offset value and copies them to another Array starting at the specified destination offset value. So BlockCopy() method takes five parameters .They are source array , source offset value , destination array , destination offset value, number of bytes to copy ( in our example we need to copy 5 elements then 5 x 4 = 20 bytes is the number of bytes to copy). 

Before Buffer.BulkCopy() operation: 

Myarr1[1] Myarr1[2] Myarr1[3] Myarr3[4] Myarr4[5]
1 to 4 bytes 4 to 8 bytes 8 to 12 bytes 12 to 16 bytes 16 to 20 bytes
1(data) 2(data) 3(data) 4(data) 5(data)

Myarr2[1] Myarr2[1] Myarr2[1] Myarr2[1] Myarr2[1] Myarr2[1] Myarr2[1] Myarr2[1] Myarr2[1] Myarr2[1]
1 to 4 bytes 4 to 8 bytes 8 to 12 bytes 12 to 16 bytes 16 to 20 bytes 20 to 24 bytes 24 to 28 bytes 28 to 32 bytes 32 to 36 bytes 36 to 40 bytes
0(data) 0(data) 0(data) 0(data) 0(data) 6(data) 7(data) 8(data) 9(data) 10(data)
  

After Buffer.BulkCopy() operation:

Myarr1[1] Myarr1[2] Myarr1[3] Myarr3[4] Myarr4[5]
1 to 4 bytes 4 to 8 bytes 8 to 12 bytes 12 to 16 bytes 16 to 20 bytes
1(data) 2(data) 3(data) 4(data) 5(data)

Myarr2[1] Myarr2[1] Myarr2[1] Myarr2[1] Myarr2[1] Myarr2[1] Myarr2[1] Myarr2[1] Myarr2[1] Myarr2[1]
1 to 4 bytes 4 to 8 bytes 8 to 12 bytes 12 to 16 bytes 16 to 20 bytes 20 to 24 bytes 24 to 28 bytes 28 to 32 bytes 32 to 36 bytes 36 to 40 bytes
1(data) 2(data) 3(data) 4(data) 5(data) 6(data) 7(data) 8(data) 9(data) 10(data)

Here is the program for using Buffer.BlockCopy(): 

using System;
namespace ConsoleApplication1
{
class buffer1
{
static void Main(string[] args)
{
int[] myarr1 = new int[5] {1,2,3,4,5};
int[] myarr2=new int[10] {0,0,0,0,0,6,7,8,9,10};
Console.Write("Before Block copy operation\n");
Console.Write("Myarr1 and Byte Length :{0}\n",Buffer.ByteLength(myarr1));
foreach(int i in myarr1)
Console.Write("{0} \t",i);
Console.WriteLine("\nMyarr2 and Byte Length:{0} \n",Buffer.ByteLength(myarr2));
foreach(int i in myarr2)
Console.Write("{0} \t",i);
Buffer.BlockCopy(myarr1,0,myarr2,0,20);
Console.Write("After Block copy operation\n");
Console.Write("Myarr1 :\n");
foreach(int i in myarr1)
Console.Write("{0} \t",i);
Console.WriteLine("\nMyarr2: \n");
foreach(int i in myarr2)
Console.Write("{0} \t",i);
Console.ReadLine();
}
}
}

Output:

Before Block Copy operation

Myarr1 and Byte Length :20

1       2       3       4       5

Myarr2 and Byte Length:40

0       0       0       0       0       6       7       8       9       10

After Block Copy operation

Myarr1 :

1       2       3       4       5

Myarr2:

1       2       3       4       5       6       7       8       9       10


Similar Articles