Updated April 14, 2023
Introduction to C# Buffer
The meaning of the word buffer is something that works on the memory directly and the process of manipulating the memory which is unmanaged and is in the form of an array of bytes representation is called buffering in C#. And the members of the buffer class in C# are BlockCopy() which copies the bytes from one array starting from a given location to another array starting from a given location, ByteLength() using which the total number of bytes in the array can be obtained, GetByte() using which a byte at the given location can be obtained and SetByte() using which a byte can be set at the given location in the array.
Syntax:
Buffer.Buffer_member_name(parameters);
Where Buffer_member_name is the name of the members of the buffer class and
Parameters are the parameters passed to it.
Working on C# Buffer
- Whenever there is a need to work on the memory directly and more specifically if we want to manipulate the memory that is unmanaged is in the form of bytes array representation, we make use of Buffer in C#.
- The Buffer class consists of several buffer members which are GetByte(), SetByte(), BlockCopy() and ByteLength().
- GetByte() is a Buffer member of the Buffer class using which a byte at the given location can be obtained.
- SetByte() is a Buffer member of the Buffer class using which a byte can be set at the given location in the array.
- BlockCopy() is a Buffer member which copies the bytes from one array starting from a given location to another array starting from a given location.
- ByteLength() is a Buffer member using which the total number of bytes in the array can be obtained.
Members of C# Buffer Class
There are four members of the Buffer class. They are:
1. BlockCopy()
BlockCopy() is a Buffer member which copies the bytes from one array starting from a given location to another array starting from a given location.
2. ByteLength()
ByteLength() is a Buffer member using which the total number of bytes in the array can be obtained.
Below is the example:
C# program to demonstrate Buffer members of the class ByteCopy() and ByteLength() members to copy the bytes from one array starting from a given location to another array starting from a given location:
Code:
using System;
//a class called program is defined
public class program
{
//main method is called
public static void Main(string[] args)
{
//an integer array is defined to store 4 integers
int[] strarray1 = new int[4] { 100, 200, 300, 400 };
//another integer array is defined to store 7 integers
int[] strarray2 = new int[7] { 0,0,0,0,500,600,700 };
Console.Write("The contents of the string array one before performing the Block operation is:\n");
//Bytelength() member of buffer class is used to find the bytelength of th given array
Console.Write("The name of the array is strarray1 and the byte length of the array is :{0}\n", Buffer.ByteLength(strarray1));
for (int j = 0; j < strarray1.Length; j++)
{
Console.Write(strarray1[j]);
Console.Write("\n");
}
Console.Write("The contents of the string array two before performing the Block copy operation is:\n");
Console.Write("The name of the array is strarray2 and the byte length of the array is :{0}\n", Buffer.ByteLength(strarray2));
for (int a = 0; a < strarray2.Length; a++)
{
Console.Write(strarray2[a]);
Console.Write("\n");
}
//Blockcopy() member of buffer class is used to copy the contents of one array starting from the location specified by the second parameter to another array starting from the location specified by fourth parameter and last parameter signifies the bytelength of the first array
Buffer.BlockCopy(strarray1, 0, strarray2, 0,Buffer.ByteLength(strarray1));
Console.Write("The contents of the string array one after performing the block copy operation is:\n");
Console.Write("The name of the array is strarray1 and the contents are :\n");
for (int b = 0; b < strarray1.Length; b++)
{
Console.Write(strarray1[b]);
Console.Write("\n");
}
Console.Write("The contents of the string array two after performing the block copy operation is:\n");
Console.Write("The name of the array is strarray2 and the contents are :\n");
for (int d = 0; d < strarray2.Length; d++) {
Console.Write(strarray2[d]);
Console.Write("\n");
}
}
}
Output:
Explanation: In the above program, a class called program is defined. Then the main method is called within which two integer arrays of different sizes are defined to store the integers. The content of the first array is displayed and the byte length of the first array is displayed using ByteLength member of the Buffer class. Then the contents of the second array is displayed and the byte length of the second array is displayed using ByteLength member of the Buffer class. Then Blockcopy() member of the buffer class is used to copy the contents of one array starting from the location specified by the second parameter to another array starting from the location specified by the fourth parameter and the last parameter signifies the bytelength of the first array. Then the contents of the first array after block copy operation is displayed. Then the contents of the second array after the block copy operation is displayed.
3. SetByte()
SetByte() is a Buffer member of the Buffer class using which a byte can be set at the given location in the array.
4. GetByte()
GetByte()is a Buffer member of the Buffer class using which a byte at the given location can be obtained.
Below is the example:
C# program to demonstrate Buffer members of the class SetByte() and GetByte() members:
Code:
using System;
//a class called check is defined
class check
{
//main method is called
static void Main()
{
//an inetger array is used to store the integers whose byte values are obtained by using GetByte member of buffer class
int[] arrayname = { 0, 1, 512 };
for (inti = 0; i<Buffer.ByteLength(arrayname); i++)
{
Console.WriteLine(Buffer.GetByte(arrayname, i));
}
// SetByte member of buffer class is used to set the byte values of the array
Buffer.SetByte(arrayname, 0, 10);
Buffer.SetByte(arrayname, 4, 20);
Buffer.SetByte(arrayname, 8, 30);
// The modified array after using SetByte member of the Buffer class is displayed
Console.WriteLine("The modified array after using SetByte member of the Buffer class is:");
for (inti = 0; i<Buffer.ByteLength(arrayname); i++)
{
Console.WriteLine(Buffer.GetByte(arrayname, i));
}
}
}
Output:
Explanation: In the above program, a class called check is defined. Then the main method is called within which an integer array is used to store the integers whose byte values are obtained by using GetByte member of buffer class. Then SetByte member of buffer class is used to set the byte values of the array. Then the modified array after using SetByte member of the Buffer class is displayed. The output is shown in the snapshot above.
Conclusion
In this tutorial, we understand the concept of Buffer in C# through definition, syntax, working, and members of the buffer class through programming examples and their outputs.
Recommended Article
This is a guide to C# Buffer. Here we discuss the Introduction to C# Buffer and its working along with its examples and Code Implementation. You can also go through our other suggested articles to learn more –