Export (0) Print
Expand All

Marshal.ReadByte Method

Reads a single byte from an unmanaged pointer.

Overload List

Reads a single byte from an unmanaged pointer.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ReadByte(IntPtr) As Byte
[C#] public static byte ReadByte(IntPtr);
[C++] public: static unsigned char ReadByte(IntPtr);
[JScript] public static function ReadByte(IntPtr) : Byte;

Reads a single byte at a given offset (or index) from an unmanaged pointer.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ReadByte(IntPtr, Integer) As Byte
[C#] public static byte ReadByte(IntPtr, int);
[C++] public: static unsigned char ReadByte(IntPtr, int);
[JScript] public static function ReadByte(IntPtr, int) : Byte;

Reads a single byte from an unmanaged pointer.

[Visual Basic] Overloads Public Shared Function ReadByte(Object, Integer) As Byte
[C#] public static byte ReadByte(object, int);
[C++] public: static unsigned char ReadByte(Object, int);
[JScript] public static function ReadByte(Object, int) : Byte;

Example

[Visual Basic, C#] The following example compares two ways of interacting with an unmanaged C-style byte array. The ReadByte method provides direct access to the element values of the array.

[Visual Basic, C#] Note   This example shows how to use one of the overloaded versions of ReadByte. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Dim unmanagedArray As IntPtr = ...
Dim i As Integer
' One way to print the 10 elements of the C-style unmanagedArray
Dim newArray As Byte(9)
Marshal.Copy(unmanagedArray, newArray, 0, 10)
For i = 0 To newArray.Length
  Console.WriteLine(newArray(i))
Next i
' Another way to print the 10 elements of the C-style unmanagedArray
For i = 0 To 10
  Console.WriteLine(Marshal.ReadByte(unmanagedArray, i))
Next i
      
[C#] 
IntPtr unmanagedArray = ...;
// One way to print the 10 elements of the C-style unmanagedArray
byte [] newArray = new byte[10];
Marshal.Copy(unmanagedArray, newArray, 0, 10);
for (int i = 0; i < newArray.Length; i++)
  Console.WriteLine(newArray[i]);
// Another way to print the 10 elements of the C-style unmanagedArray
for (int i = 0; i < 10; i++)
  Console.WriteLine(Marshal.ReadByte(unmanagedArray, i));
      

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Marshal Class | Marshal Members | System.Runtime.InteropServices Namespace

Show:
© 2014 Microsoft