UnmanagedMemoryStream Constructor (Byte*, Int64, Int64, FileAccess)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Initializes a new instance of the UnmanagedMemoryStream class using the specified location, memory length, total amount of memory, and file access values.

This API is not CLS-compliant.

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

public unsafe UnmanagedMemoryStream(
	byte* pointer,
	long length,
	long capacity,
	FileAccess access


Type: System.Byte*

A pointer to an unmanaged memory location.

Type: System.Int64

The length of the memory to use.

Type: System.Int64

The total amount of memory assigned to the stream.

Type: System.IO.FileAccess

One of the FileAccess values.

Exception Condition

The user does not have the required permission.


The pointer value is null.


The length value is less than zero.

- or -

The capacity value is less than zero.

- or -

The length value is greater than the capacity value.

The length parameter defines the current amount of memory in use. If reading or appending data to the stream, the length value should be equal to the amount of valid data in the stream to be read from or preserved. If writing to the stream, this value should be zero.

The capacity parameter indicates the amount of total memory available. This value can describe a region that is longer than the length specified, or indicate a region that can be appended to. Any attempt to write beyond this value will fail.

The access parameter sets the CanRead, and CanWrite properties. Note that specifying Write does not guarantee that the stream will be writable. The access parameters allow the implementer to create an object whose implementation can match the actual stream that is exposed.

The following code example demonstrates how to read from and write to unmanaged memory using the UnmanagedMemoryStream class. A block of unmanaged memory is allocated and de-allocated using the Marshal class.

// Note: you must compile this sample using the unsafe flag.
// From the command line, type the following: csc sample.cs /unsafe

using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;

unsafe class TestWriter

    static void Main()

            // Create some data to read and write.
            byte[] message = UnicodeEncoding.Unicode.GetBytes("Here is some data.");

	    // Allocate a block of unmanaged memory and return an IntPtr object.	
            IntPtr memIntPtr = Marshal.AllocHGlobal(message.Length);

            // Get a byte pointer from the IntPtr object.
            byte* memBytePtr = (byte*) memIntPtr.ToPointer();

            // Create an UnmanagedMemoryStream object using a pointer to unmanaged memory.
            UnmanagedMemoryStream writeStream = new UnmanagedMemoryStream(memBytePtr, message.Length, message.Length, FileAccess.Write);

            // Write the data.
            writeStream.Write(message, 0, message.Length);

            // Close the stream.

            // Create another UnmanagedMemoryStream object using a pointer to unmanaged memory.
            UnmanagedMemoryStream readStream = new UnmanagedMemoryStream(memBytePtr, message.Length, message.Length, FileAccess.Read);

	    // Create a byte array to hold data from unmanaged memory.
            byte[] outMessage = new byte[message.Length];

            // Read from unmanaged memory to the byte array.
            readStream.Read(outMessage, 0, message.Length);

            // Close the stream.

            // Display the data to the console.

            // Free the block of unmanaged memory.



for accessing unmanaged memory.


requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

Universal Windows Platform
Available since 10
.NET Framework
Available since 2.0
Return to top