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();
public MemoryStream ();
Public Sub New ()

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

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(cli::array <System::Byte> ^ buffer);
public MemoryStream (byte[] buffer);
new System.IO.MemoryStream : byte[] -> System.IO.MemoryStream
Public Sub New (buffer As Byte())

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

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);
public MemoryStream (int capacity);
new System.IO.MemoryStream : int -> System.IO.MemoryStream
Public Sub New (capacity As Integer)

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.

MemoryStream^ memStream = gcnew MemoryStream( 100 );
using(MemoryStream memStream = new MemoryStream(100))
Dim memStream As 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

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(cli::array <System::Byte> ^ buffer, bool writable);
public MemoryStream (byte[] buffer, bool writable);
new System.IO.MemoryStream : byte[] * bool -> System.IO.MemoryStream
Public Sub New (buffer As Byte(), writable As Boolean)

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

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(cli::array <System::Byte> ^ buffer, int index, int count);
public MemoryStream (byte[] buffer, int index, int count);
new System.IO.MemoryStream : byte[] * int * int -> System.IO.MemoryStream
Public Sub New (buffer As Byte(), index As Integer, count As Integer)

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

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(cli::array <System::Byte> ^ buffer, int index, int count, bool writable);
public MemoryStream (byte[] buffer, int index, int count, bool writable);
new System.IO.MemoryStream : byte[] * int * int * bool -> System.IO.MemoryStream
Public Sub New (buffer As Byte(), index As Integer, count As Integer, writable As Boolean)

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

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(cli::array <System::Byte> ^ buffer, int index, int count, bool writable, bool publiclyVisible);
public MemoryStream (byte[] buffer, int index, int count, bool writable, bool publiclyVisible);
new System.IO.MemoryStream : byte[] * int * int * bool * bool -> System.IO.MemoryStream
Public Sub New (buffer As Byte(), index As Integer, count As Integer, writable As Boolean, publiclyVisible As Boolean)

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