Marshal.ReadByte Method

Definition

Reads a single byte from unmanaged memory. Reading from unaligned memory locations is supported.

Overloads

ReadByte(IntPtr, Int32)

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

ReadByte(Object, Int32)
Obsolete.

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

ReadByte(IntPtr)

Reads a single byte from unmanaged memory.

ReadByte(IntPtr, Int32)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

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

C#
[System.Security.SecurityCritical]
public static byte ReadByte(IntPtr ptr, int ofs);
C#
public static byte ReadByte(IntPtr ptr, int ofs);

Parameters

ptr
IntPtr

The base address in unmanaged memory from which to read.

ofs
Int32

An additional byte offset, which is added to the ptr parameter before reading.

Returns

The byte read from unmanaged memory at the given offset.

Attributes

Exceptions

Base address (ptr) plus offset byte (ofs) produces a null or invalid address.

Examples

The following example demonstrates how to read and write to an unmanaged array using the ReadByte and WriteByte methods.

C#
static void ReadWriteByte()
{
    // Allocate unmanaged memory. 
    int elementSize = 1;
    IntPtr unmanagedArray = Marshal.AllocHGlobal(10 * elementSize);

    // Set the 10 elements of the C-style unmanagedArray
    for (int i = 0; i < 10; i++)
    {
        Marshal.WriteByte(unmanagedArray, i * elementSize, ((Byte)(i + 1)));
    }
    Console.WriteLine("Unmanaged memory written.");

    Console.WriteLine("Reading unmanaged memory:");
    // Print the 10 elements of the C-style unmanagedArray
    for (int i = 0; i < 10; i++)
    {
        Console.WriteLine(Marshal.ReadByte(unmanagedArray, i * elementSize));
    }

    Marshal.FreeHGlobal(unmanagedArray);

    Console.WriteLine("Done. Press Enter to continue.");
    Console.ReadLine();
}

The following example demonstrates how to use the ReadByte method to read the value of an unmanaged character.

C++

using namespace System;
using namespace System::Runtime::InteropServices;



void main()
{
    // Create an unmanaged byte.
    const char * myString = "bB";

    // Read the second character of the c string as a managed byte.
        Byte ^ myManagedByte = Marshal::ReadByte((IntPtr) (char *) myString, 1);

    // Display the byte to the console.
    Console::WriteLine(myManagedByte);
}

Remarks

ReadByte enables direct interaction with an unmanaged C-style byte array, eliminating the expense of copying an entire unmanaged array (using Marshal.Copy) to a separate managed array before reading its element values.

Reading from unaligned memory locations is supported.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ReadByte(Object, Int32)

Source:
Marshal.CoreCLR.cs
Source:
Marshal.CoreCLR.cs
Source:
Marshal.CoreCLR.cs

Caution

ReadByte(Object, Int32) may be unavailable in future releases.

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

C#
[System.Obsolete("ReadByte(Object, Int32) may be unavailable in future releases.")]
[System.Security.SecurityCritical]
public static byte ReadByte(object ptr, int ofs);
C#
[System.Obsolete("ReadByte(Object, Int32) may be unavailable in future releases.")]
public static byte ReadByte(object ptr, int ofs);
C#
public static byte ReadByte(object ptr, int ofs);
C#
[System.Security.SecurityCritical]
public static byte ReadByte(object ptr, int ofs);

Parameters

ptr
Object

The base address in unmanaged memory of the source object.

ofs
Int32

An additional byte offset, which is added to the ptr parameter before reading.

Returns

The byte read from unmanaged memory at the given offset.

Attributes

Exceptions

Base address (ptr) plus offset byte (ofs) produces a null or invalid address.

ptr is an ArrayWithOffset object. This method does not accept ArrayWithOffset parameters.

Remarks

ReadByte enables direct interaction with an unmanaged C-style byte array, eliminating the expense of copying an entire unmanaged array (using Marshal.Copy) to a separate managed array before reading its element values.

Reading from unaligned memory locations is supported.

See also

Applies to

.NET 9 and other versions
Product Versions (Obsolete)
.NET (Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 (4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided))
.NET Standard 1.1, 2.0 (1.2, 1.3, 1.4, 1.5, 1.6, 2.1)
UWP (10.0)

ReadByte(IntPtr)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Reads a single byte from unmanaged memory.

C#
[System.Security.SecurityCritical]
public static byte ReadByte(IntPtr ptr);
C#
public static byte ReadByte(IntPtr ptr);

Parameters

ptr
IntPtr

The address in unmanaged memory from which to read.

Returns

The byte read from unmanaged memory.

Attributes

Exceptions

ptr is not a recognized format.

-or-

ptr is null.

-or-

ptr is invalid.

Examples

The following example creates a block of unmanaged memory, writes a byte to the unmanaged memory, reads the byte back from unmanaged memory, and then disposes the unmanaged memory.

C#
using System;
using System.Runtime.InteropServices;

 class Example
 {
     static void Main(string[] args)
     {
          // Allocate 1 byte of unmanaged memory.
          IntPtr hGlobal = Marshal.AllocHGlobal(1);

          // Create a new byte.
          byte b = 1;
          Console.WriteLine("Byte written to unmanaged memory: " + b);

          // Write the byte to unmanaged memory.
          Marshal.WriteByte(hGlobal, b);

          // Read byte from unmanaged memory.
          byte c = Marshal.ReadByte(hGlobal);
          Console.WriteLine("Byte read from unmanaged memory: " + c);

          // Free the unmanaged memory.
          Marshal.FreeHGlobal(hGlobal);
          Console.WriteLine("Unmanaged memory was disposed.");
     }
}

The following example demonstrates how to use the ReadByte method to read the value of an unmanaged character.

C++

using namespace System;
using namespace System::Runtime::InteropServices;



void main()
{
    // Create an unmanaged byte.
    const char * myString = "b";

    // Read the c string as a managed byte.
        Byte ^ myManagedByte = Marshal::ReadByte((IntPtr) (char *) myString);

    // Display the byte to the console.
    Console::WriteLine(myManagedByte);
}

Remarks

ReadByte has an implied offset of 0. This method enables direct interaction with an unmanaged C-style byte array, eliminating the expense of copying an entire unmanaged array (using Marshal.Copy) to a separate managed array before reading its element values.

Reading from unaligned memory locations is supported.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0