Files and Streams
Memory Stream
Write bytes to memory and read back stream facts.
Memory Stream
MemoryStreamExample.cs
using System;
using System.IO;
class Program
{
static void Main()
{
int firstByte = ;
using MemoryStream stream = new MemoryStream();
stream.WriteByte((byte)firstByte);
stream.WriteByte(90);
long length = stream.Length;
int first = (int)stream.ToArray()[0];
Console.WriteLine($"length={length}");
Console.WriteLine($"first={first}");
}
}
using System;
using System.IO;
class Program
{
static void Main()
{
int firstByte = ;
using MemoryStream stream = new MemoryStream();
stream.WriteByte((byte)firstByte);
stream.WriteByte(90);
long length = stream.Length;
int first = (int)stream.ToArray()[0];
Console.WriteLine($"length={length}");
Console.WriteLine($"first={first}");
}
}
using System;
using System.IO;
class Program
{
static void Main()
{
int firstByte = ;
using MemoryStream stream = new MemoryStream();
stream.WriteByte((byte)firstByte);
stream.WriteByte(90);
long length = stream.Length;
int first = (int)stream.ToArray()[0];
Console.WriteLine($"length={length}");
Console.WriteLine($"first={first}");
}
}
streams
A `MemoryStream` stores bytes in memory instead of on disk.