// writing string to MemoryStream string message = "Hello, World!"; byte[] buffer = Encoding.UTF8.GetBytes(message); using (var stream = new MemoryStream()) { stream.Write(buffer, 0, buffer.Length); // do something with the stream }
// writing binary data to MemoryStream byte[] data = { 0x12, 0x34, 0x56, 0x78 }; using (var stream = new MemoryStream()) { stream.Write(data, 0, data.Length); // do something with the stream }In this example, we create a `MemoryStream` object and write an array of binary data to it using the `Write` method. We specify the starting index and length of the data to write in the method arguments. Package library: `System.IO`, which is part of the .NET Framework.