Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

System.IO.MemoryStream: practical reference

System.IO.MemoryStream: practical reference: MemoryStream is an in-memory byte stream with Position, Length, and Capacity.

Version: .NET API; verified on .NET 10.0.14

Syntax

System.IO.MemoryStream /* select a documented member or constructor */

MemoryStream is an in-memory byte stream with Position, Length, and Capacity.

Use it for small or medium buffers that need the Stream API without file I/O.

ToArray copies, GetBuffer can expose unused capacity, and Dispose does not erase sensitive data.

Seek beyond Length only changes Position; it neither extends the stream nor creates bytes. A later extending write or SetLength zero-fills the gap, and a stream over an existing array may be non-expandable.

The complete program actually uses System.IO.MemoryStream; its exact expected stdout is 65.

Examples

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

using var stream = new MemoryStream();
stream.WriteByte(65);
Console.WriteLine(stream.ToArray()[0]);

Result

65

The complete program actually uses `System.IO.MemoryStream`; its exact expected stdout is `65`.

Sources