using (FileStream fileStream = new FileStream("example.bin", FileMode.Create)) using (BinaryWriter writer = new BinaryWriter(fileStream)) { writer.Write(123); writer.AlignStream(4); // Align to the nearest multiple of 4 writer.Write(456); }
using (MemoryStream memoryStream = new MemoryStream()) using (BinaryWriter writer = new BinaryWriter(memoryStream)) { writer.Write("hello"); writer.AlignStream(8); // Align to the nearest multiple of 8 writer.Write(true); }In this example, we are writing a string and a boolean value to a memory stream. We use the AlignStream method to align the stream to the nearest multiple of 8 bytes, which ensures that the boolean value is properly aligned in the stream. The BinaryWriter class is part of the System.IO namespace in the .NET Framework. It is used for writing binary data to a stream.