MemoryStream MemoryStream MemoryStream MemoryStream Constructors

Definition

Overloads

MemoryStream() MemoryStream() MemoryStream()

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

MemoryStream(Byte[]) MemoryStream(Byte[]) MemoryStream(Byte[]) MemoryStream(Byte[])

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

MemoryStream(Int32) MemoryStream(Int32) MemoryStream(Int32) MemoryStream(Int32)

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

MemoryStream(Byte[], Boolean) MemoryStream(Byte[], Boolean) MemoryStream(Byte[], Boolean) 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) MemoryStream(Byte[], Int32, Int32) MemoryStream(Byte[], Int32, Int32) 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) MemoryStream(Byte[], Int32, Int32, Boolean) MemoryStream(Byte[], Int32, Int32, Boolean) 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) MemoryStream(Byte[], Int32, Int32, Boolean, Boolean) MemoryStream(Byte[], Int32, Int32, Boolean, Boolean) 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() MemoryStream() MemoryStream()

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.

MemoryStream(Byte[]) MemoryStream(Byte[]) MemoryStream(Byte[]) MemoryStream(Byte[])

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

public:
 MemoryStream(cli::array <System::Byte> ^ buffer);
[System.Runtime.TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
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

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.

MemoryStream(Int32) MemoryStream(Int32) MemoryStream(Int32) MemoryStream(Int32)

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 Int32 Int32 Int32

The initial size of the internal array in bytes.

Exceptions

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.

MemoryStream(Byte[], Boolean) MemoryStream(Byte[], Boolean) MemoryStream(Byte[], Boolean) 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.

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 Boolean Boolean Boolean

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

Exceptions

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.

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

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 Int32 Int32 Int32

The index into buffer at which the stream begins.

count
Int32 Int32 Int32 Int32

The length of the stream in bytes.

Exceptions

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.

MemoryStream(Byte[], Int32, Int32, Boolean) MemoryStream(Byte[], Int32, Int32, Boolean) MemoryStream(Byte[], Int32, Int32, Boolean) 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.

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 Int32 Int32 Int32

The index in buffer at which the stream begins.

count
Int32 Int32 Int32 Int32

The length of the stream in bytes.

writable
Boolean Boolean Boolean Boolean

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

Exceptions

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.

MemoryStream(Byte[], Int32, Int32, Boolean, Boolean) MemoryStream(Byte[], Int32, Int32, Boolean, Boolean) MemoryStream(Byte[], Int32, Int32, Boolean, Boolean) 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.

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 Int32 Int32 Int32

The index into buffer at which the stream begins.

count
Int32 Int32 Int32 Int32

The length of the stream in bytes.

writable
Boolean Boolean Boolean Boolean

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

publiclyVisible
Boolean Boolean Boolean Boolean

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

Exceptions

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).

Applies to