Click here to Skip to main content
Licence 
First Posted 17 Dec 2001
Views 63,255
Bookmarked 17 times

Understanding the System.Buffer class

By | 17 Dec 2001 | Article
A brief introduction to the .NET System.Buffer class

Introduction:

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

The word Buffer itself says that it works on direct Memory. In .NET it’s basically a manipulation of unmanaged memory represented as arrays of bytes. So lets take an 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 the 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 the number of elements in array. In our example Myarr1 has 5 elements so the array length is 5. And Myarr2 has 10 elements so the array length is 10.

The Array class has the Copy() method which copies one arrays 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 the Copy() method takes five parameters .They are source array , source index, destination array , destination index, number of elements to copy.

Note: I am taking array index starting from 1 for better understanding in examples

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[2]

Myarr2[3]

Myarr2[4]

Myarr2[5]

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[2]

Myarr2[3]

Myarr2[4]

Myarr2[5]

Myarr2[6]

Myarr2[7]

Myarr2[8]

Myarr2[9]

Myarr2[10]

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
      {
            staticvoid Main(string[] args)
            {
                 
            int[] myarr1 = newint[5] {1,2,3,4,5};
            int[] myarr2=newint[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);

            //here we are copying index to index as data
            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[2]

Myarr2[3]

Myarr2[4]

Myarr2[5]

Myarr2[6]

Myarr2[7]

Myarr2[8]

Myarr2[9]

Myarr2[10]

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)

AfterBuffer.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[2]

Myarr2[3]

Myarr2[4]

Myarr2[5]

Myarr2[6]

Myarr2[7]

Myarr2[8]

Myarr2[9]

Myarr2[10]

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
        {
               staticvoid Main(string[] args)
               {
                      
               int[] myarr1 = newint[5] {1,2,3,4,5};
               int[] myarr2=newint[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);
              
               //here we are copying offset to offet as bytes
               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

If you observe the both example they do give same result but the way they operate is completely different.

In example 1 we used Array.Copy(myarr1,0,myarr2,0,5);--> here 5 means number array elements to copy
In example 2 we used Buffer.BlockCopy(myarr1,0,myarr2,0,20);--> here 20 means number of bytes to copy

So in our example 5 elements of int occupies 20 bytes.

Further reading:

  1. Microsoft:  More information on .NET technologies

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Chandra Hundigam

Architect

United States United States

Member

Chandra Hundigam has Master degree in Computer Application, Microsoft Certified Professional and Software Architect. He's significantly involved in enterprise application development and distributed object oriented system development using Microsoft .Net, Sun Java/J2EE technology to serve global giants in the Media, Finance, Mortgage and Software Industries.Presently working as Independent Software Consultant for a US-based company.His areas of interests are in emerging Technologies.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThanks Pinmemberatarikg16:49 19 Jan '10  
Generalexcellent Pinmembersheikh shareef0:29 25 Apr '05  
GeneralUsing windows Api to disable Alt+Ctrl+Del in C# Pinmemberrm_babar9:19 25 Apr '03  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 18 Dec 2001
Article Copyright 2001 by Chandra Hundigam
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid