MemoryStream Constructors

Definition

Initializes a new instance of the MemoryStream class.

Overloads

MemoryStream()

Initializes a new instance of the MemoryStream class with an expandable capacity initialized to zero.

MemoryStream(Byte[])

Initializes a new non-resizable instance of the MemoryStream class based on the specified byte array.

MemoryStream(Int32)

Initializes a new instance of the MemoryStream class with an expandable capacity initialized as specified.

MemoryStream(Byte[], Boolean)

Initializes a new non-resizable instance of the MemoryStream class based on the specified byte array with the CanWrite property set as specified.

MemoryStream(Byte[], Int32, Int32)

Initializes a new non-resizable instance of the MemoryStream class based on the specified region (index) of a byte array.

MemoryStream(Byte[], Int32, Int32, Boolean)

Initializes a new non-resizable instance of the MemoryStream class based on the specified region of a byte array, with the CanWrite property set as specified.

MemoryStream(Byte[], Int32, Int32, Boolean, Boolean)

Initializes a new instance of the MemoryStream class based on the specified region of a byte array, with the CanWrite property set as specified, and the ability to call GetBuffer() set as specified.

MemoryStream()

Source:
MemoryStream.cs
Source:
MemoryStream.cs
Source:
MemoryStream.cs

Initializes a new instance of the MemoryStream class with an expandable capacity initialized to zero.

public MemoryStream ();

Remarks

The CanRead, CanSeek, and CanWrite properties are all set to true.

The capacity of the current stream automatically increases when you use the SetLength method to set the length to a value larger than the capacity of the current stream.

This constructor exposes the underlying stream, which GetBuffer returns.

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.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

MemoryStream(Byte[])

Source:
MemoryStream.cs
Source:
MemoryStream.cs
Source:
MemoryStream.cs

Initializes a new non-resizable instance of the MemoryStream class based on the specified byte array.

public MemoryStream (byte[] buffer);

Parameters

buffer
Byte[]

The array of unsigned bytes from which to create the current stream.

Exceptions

buffer is null.

Remarks

The CanRead, CanSeek, and CanWrite properties are all set to true. Capacity is set to the length of the specified byte array. The new stream can be written to, but is not resizable.

The length of the stream cannot be set to a value greater than the initial length of the specified byte array; however, the stream can be truncated (see SetLength).

This constructor does not expose the underlying stream. GetBuffer throws UnauthorizedAccessException.

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.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

MemoryStream(Int32)

Source:
MemoryStream.cs
Source:
MemoryStream.cs
Source:
MemoryStream.cs

Initializes a new instance of the MemoryStream class with an expandable capacity initialized as specified.

public MemoryStream (int capacity);

Parameters

capacity
Int32

The initial size of the internal array in bytes.

Exceptions

capacity is negative.

Examples

This code example is part of a larger example provided for the MemoryStream class.

using(MemoryStream memStream = new MemoryStream(100))

Remarks

The CanRead, CanSeek, and CanWrite properties are all set to true.

The capacity automatically increases when you use the SetLength method to set the length to a value larger than the capacity of the current stream. Except for a MemoryStream constructed with a byte[] parameter, write operations at the end of a MemoryStream expand the MemoryStream.

This constructor exposes the underlying stream that GetBuffer returns.

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.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

MemoryStream(Byte[], Boolean)

Source:
MemoryStream.cs
Source:
MemoryStream.cs
Source:
MemoryStream.cs

Initializes a new non-resizable instance of the MemoryStream class based on the specified byte array with the CanWrite property set as specified.

public MemoryStream (byte[] buffer, bool writable);

Parameters

buffer
Byte[]

The array of unsigned bytes from which to create this stream.

writable
Boolean

The setting of the CanWrite property, which determines whether the stream supports writing.

Exceptions

buffer is null.

Remarks

The CanRead and CanSeek properties are both set to true. Capacity is set to the length of the specified byte array.

The length of the stream cannot be set to a value greater than the initial length of the specified byte array; however, the stream can be truncated (see SetLength).

This constructor does not expose the underlying stream. GetBuffer throws UnauthorizedAccessException.

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.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

MemoryStream(Byte[], Int32, Int32)

Source:
MemoryStream.cs
Source:
MemoryStream.cs
Source:
MemoryStream.cs

Initializes a new non-resizable instance of the MemoryStream class based on the specified region (index) of a byte array.

public MemoryStream (byte[] buffer, int index, int count);

Parameters

buffer
Byte[]

The array of unsigned bytes from which to create this stream.

index
Int32

The index into buffer at which the stream begins.

count
Int32

The length of the stream in bytes.

Exceptions

buffer is null.

index or count is less than zero.

The buffer length minus index is less than count.

Remarks

The CanRead, CanSeek, and CanWrite properties are all set to true, but the capacity cannot be changed. Capacity is set to count.

The length of the stream cannot be set to a value greater than the initial length of the specified byte array; however, the stream can be truncated (see SetLength).

This constructor does not expose the underlying stream. GetBuffer throws UnauthorizedAccessException. However, you can write to the stream.

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.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

MemoryStream(Byte[], Int32, Int32, Boolean)

Source:
MemoryStream.cs
Source:
MemoryStream.cs
Source:
MemoryStream.cs

Initializes a new non-resizable instance of the MemoryStream class based on the specified region of a byte array, with the CanWrite property set as specified.

public MemoryStream (byte[] buffer, int index, int count, bool writable);

Parameters

buffer
Byte[]

The array of unsigned bytes from which to create this stream.

index
Int32

The index in buffer at which the stream begins.

count
Int32

The length of the stream in bytes.

writable
Boolean

The setting of the CanWrite property, which determines whether the stream supports writing.

Exceptions

buffer is null.

index or count are negative.

The buffer length minus index is less than count.

Remarks

The CanRead and CanSeek properties are both set to true. Capacity is set to count.

The length of the stream cannot be set to a value greater than the initial length of the specified byte array; however, the stream can be truncated (see SetLength).

This constructor does not expose the underlying stream. GetBuffer throws UnauthorizedAccessException. However, you can write to the stream if writable is true.

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.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

MemoryStream(Byte[], Int32, Int32, Boolean, Boolean)

Source:
MemoryStream.cs
Source:
MemoryStream.cs
Source:
MemoryStream.cs

Initializes a new instance of the MemoryStream class based on the specified region of a byte array, with the CanWrite property set as specified, and the ability to call GetBuffer() set as specified.

public MemoryStream (byte[] buffer, int index, int count, bool writable, bool publiclyVisible);

Parameters

buffer
Byte[]

The array of unsigned bytes from which to create this stream.

index
Int32

The index into buffer at which the stream begins.

count
Int32

The length of the stream in bytes.

writable
Boolean

The setting of the CanWrite property, which determines whether the stream supports writing.

publiclyVisible
Boolean

true to enable GetBuffer(), which returns the unsigned byte array from which the stream was created; otherwise, false.

Exceptions

buffer is null.

index or count is negative.

The buffer length minus index is less than count.

Remarks

The CanRead and CanSeek properties are both set to true. Capacity is set to count.

The new stream instance can be written to, but the Capacity of the underlying byte array cannot be changed. The length of the stream cannot be set to a value greater than the initial length of the specified byte array; however, the stream can be truncated (see SetLength).

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.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0