Compartilhar via


Buffer Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Manipulates arrays of primitive types.

Inheritance Hierarchy

System.Object
  System.Buffer

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
<ComVisibleAttribute(True)> _
Public NotInheritable Class Buffer
[ComVisibleAttribute(true)]
public static class Buffer

The Buffer type exposes the following members.

Methods

  Name Description
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 BlockCopy Copies a specified number of bytes from a source array starting at a particular offset to a destination array starting at a particular offset.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ByteLength Returns the number of bytes in the specified array.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetByte Retrieves the byte at a specified location in a specified array.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 SetByte Assigns a specified value to a byte at a particular location in a specified array.

Top

Remarks

Buffer only affects arrays of primitive types; this class does not apply to objects. Each primitive type is treated as a series of bytes without regard to any behavior or limitation associated with the primitive type.

Buffer provides methods to copy bytes from one array of primitive types to another array of primitive types, get a byte from an array, set a byte in an array, and obtain the length of an array. This class provides better performance for manipulating primitive types than similar methods in the System.Array class.

Buffer is applicable to the following primitive types: Boolean, Char, SByte, Byte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Single, and Double.

Examples

The following code example illustrates the use of several Buffer class methods.

' Example of the Buffer class methods.

Module Example

   ' Display the array elements from right to left in hexadecimal.
   Sub DisplayArray(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal arr() As Short)

      outputBlock.Text &= "  arr:"
      Dim loopX As Integer
      For loopX = arr.Length - 1 To 0 Step -1
         outputBlock.Text &= String.Format(" {0:X4}", arr(loopX))
      Next loopX
      outputBlock.Text &= vbCrLf
   End Sub

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' This array is to be modified and displayed.
      Dim arr() As Short = {258, 259, 260, 261, 262, 263, 264, _
                              265, 266, 267, 268, 269, 270, 271}
      outputBlock.Text &= _
          "This example of the Buffer class methods generates " & _
          "the following output." & vbCrLf & "Note: The " & _
          "array is displayed from right to left." & vbCrLf & vbCrLf
      outputBlock.Text &= "Initial values of array:" & vbCrLf & vbCrLf

      ' Display the initial array values and ByteLength.
      DisplayArray(outputBlock, arr)
      outputBlock.Text &= String.Format(vbCrLf & _
          "Buffer.ByteLength( arr ): {0}", _
          Buffer.ByteLength(arr)) & vbCrLf

      ' Copy a region of the array; set a byte within the array.
      outputBlock.Text &= String.Format(vbCrLf & _
          "Call these methods: " & vbCrLf & _
          "  Buffer.BlockCopy( arr, 5, arr, 16, 9 )," & vbCrLf & _
          "  Buffer.SetByte( arr, 7, 170 )." & vbCrLf) & vbCrLf

      Buffer.BlockCopy(arr, 5, arr, 16, 9)
      Buffer.SetByte(arr, 7, 170)

      ' Display the array and a byte within the array.
      outputBlock.Text &= "Final values of array:" & vbCrLf & vbCrLf
      DisplayArray(outputBlock, arr)
      outputBlock.Text &= String.Format(vbCrLf & _
          "Buffer.GetByte( arr, 26 ): {0}", _
          Buffer.GetByte(arr, 26)) & vbCrLf
   End Sub
End Module

' This example of the Buffer class methods generates the following output.
' Note: The array is displayed from right to left.
' 
' Initial values of array:
' 
'   arr: 010F 010E 010D 010C 010B 010A 0109 0108 0107 0106 0105 0104 0103 0102
' 
' Buffer.ByteLength( arr ): 28
' 
' Call these methods:
'   Buffer.BlockCopy( arr, 5, arr, 16, 9 ),
'   Buffer.SetByte( arr, 7, 170 ).
' 
' Final values of array:
' 
'   arr: 010F 0101 0801 0701 0601 0501 0109 0108 0107 0106 AA05 0104 0103 0102
' 
' Buffer.GetByte( arr, 26 ): 15
// Example of the Buffer class methods.
using System;

class Example
{
   // Display the array elements from right to left in hexadecimal.
   public static void DisplayArray(System.Windows.Controls.TextBlock outputBlock, short[] arr)
   {
      outputBlock.Text += "  arr:";
      for (int loopX = arr.Length - 1; loopX >= 0; loopX--)
         outputBlock.Text += String.Format(" {0:X4}", arr[loopX]);
      outputBlock.Text += "\n";
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // This array is to be modified and displayed.
      short[] arr = { 258, 259, 260, 261, 262, 263, 264, 
                         265, 266, 267, 268, 269, 270, 271 };

      outputBlock.Text += "This example of the Buffer class " +
          "methods generates the following output.\n" +
          "Note: The array is displayed from right to left.\n" + "\n";
      outputBlock.Text += "Initial values of array:\n" + "\n";

      // Display the initial array values and ByteLength.
      DisplayArray(outputBlock, arr);
      outputBlock.Text += String.Format("\nBuffer.ByteLength( arr ): {0}",
          Buffer.ByteLength(arr)) + "\n";

      // Copy a region of the array; set a byte within the array.
      outputBlock.Text += String.Format("\nCall these methods: \n" +
          "  Buffer.BlockCopy( arr, 5, arr, 16, 9 ),\n" +
          "  Buffer.SetByte( arr, 7, 170 ).\n") + "\n";

      Buffer.BlockCopy(arr, 5, arr, 16, 9);
      Buffer.SetByte(arr, 7, 170);

      // Display the array and a byte within the array.
      outputBlock.Text += "Final values of array:\n" + "\n";
      DisplayArray(outputBlock, arr);
      outputBlock.Text += String.Format("\nBuffer.GetByte( arr, 26 ): {0}",
          Buffer.GetByte(arr, 26)) + "\n";
   }
}

/*
This example of the Buffer class methods generates the following output.
Note: The array is displayed from right to left.

Initial values of array:

  arr: 010F 010E 010D 010C 010B 010A 0109 0108 0107 0106 0105 0104 0103 0102

Buffer.ByteLength( arr ): 28

Call these methods:
  Buffer.BlockCopy( arr, 5, arr, 16, 9 ),
  Buffer.SetByte( arr, 7, 170 ).

Final values of array:

  arr: 010F 0101 0801 0701 0601 0501 0109 0108 0107 0106 AA05 0104 0103 0102

Buffer.GetByte( arr, 26 ): 15
*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference