Overview
- Provides an in-memory implementation of a Stream.
- Meant for temporary streams.
- For example, to minimize how long you hold another stream open, such as a FileStream that locks a file.
Good practice
- Remember to call Close on a stream.
- Make sure to call Close in the right order, and not too early. See the example below, where closing ‘b’ too early would close ‘a’ as well.
Examples
Write to File
// MemoryStreams are usually temporary streams
// intended to be written to another stream later
MemoryStream a = new MemoryStream();
StreamWriter b = new StreamWriter(a);
b.WriteLine("Hello World");
b.WriteLine("Bye Bye!");
b.Flush();
FileStream c = File.Create(@"C:\Public\TestMem.txt");
a.WriteTo(c);
c.Close();
b.Close();
a.Close();
You May Also Like

Exam 70-536 Study Notes
My study notes for Microsoft exam 70-536: .NET types, collections, generics, I/O, serialization and more, with C# examples.

Starting Exam 70-536 Study
After a burnout, I'm putting my own project on hold to study for Microsoft exam 70-536.

Design Patterns - Proxy Pattern
The Proxy Pattern, and the last article in the design patterns series, now that I've finished reading Head First Design Patterns.




