Understanding System.Buffer Class
Before we try out something with the Buffer class, you should understand the 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 as compared to the Array class.
Introduction:
Before we try out something with the Buffer class, you should understand the 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 as compared to the Array class.
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]
0(data) 0(data) 0(data) 0(data) 0(data)
Myarr2[6] Myarr2[7] Myarr2[8] Myarr2[9] Myarr2[10]
6(data) 7(data) 8(data) 9(data) 10(data)
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]
1(data) 2(data) 3(data) 4(data) 5(data)
Myarr2[6] Myarr2[7] Myarr2[8] Myarr2[9] Myarr2[10]
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);
//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);
//just for wait
Console.ReadLine();
}
}
}
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
Before Buffer.BulkCopy() operation:
Myarr1[1] Myarr1[2] Myarr1[3] Myarr1[4] Myarr1[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]
1-4 bytes 4-8 bytes 8-12 bytes 12-16 bytes 16-20 bytes
0(data) 0(data) 0(data) 0(data) 0(data)
Myarr2[6] Myarr2[7] Myarr2[8] Myarr2[9] Myarr2[10]
20-24bytes 24-28bytes 28-32bytes 32-26bytes 36-40bytes
6(data) 7(data) 8(data) 9(data) 10(data)
After Buffer.BulkCopy() operation:
Myarr1[1] Myarr1[2] Myarr1[3] Myarr1[4] Myarr1[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]
1-4 bytes 4-8 bytes 8-12 bytes 12-16 bytes 16-20 bytes
1(data) 2(data) 3(data) 4(data) 5(data)
Myarr2[6] Myarr2[7] Myarr2[8] Myarr2[9] Myarr2[10]
20-24bytes 24-28bytes 28-32bytes 32-26bytes 36-40bytes
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);
//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);
//just for wait
Console.ReadLine();
}
}
}
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
Myarr1 :
1 2 3 4 5
Myarr2:
1 2 3 4 5 6 7 8 9 10
No comments:
Post a Comment