byte[] data = { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46 }; MemoryStream stream = new MemoryStream(data); string result = Encoding.UTF8.GetString(stream.ToArray());
MemoryStream stream = new MemoryStream(); StreamWriter writer = new StreamWriter(stream); writer.Write("This is a test string"); writer.Flush(); stream.Position = 0; string result = Encoding.UTF8.GetString(stream.ToArray());In this example, we create a new MemoryStream object and a StreamWriter object using the MemoryStream. We write a string to the stream and flush it, then set the position of the stream back to 0. Finally, we convert the MemoryStream object to a string using the Encoding.UTF8.GetString method. Package/Library: System.IO.MemoryStream is a part of the .NET Framework Class Library and is included in the System.IO namespace.