byte[] buffer = new byte[1024]; MemoryStream stream = new MemoryStream(buffer); string message = stream.readUnicode(); Console.WriteLine(message);
byte[] buffer = new byte[1024]; MemoryStream stream = new MemoryStream(buffer); // Write a string to the memory stream string message = "Hello, world!"; byte[] messageBytes = Encoding.Unicode.GetBytes(message); stream.Write(messageBytes, 0, messageBytes.Length); // Move the stream position to the beginning stream.Seek(0, SeekOrigin.Begin); // Read the string from the memory stream string readMessage = stream.readUnicode(); Console.WriteLine(readMessage);In this example, we first write a string to the memory stream by converting it to Unicode and using the Write method of the MemoryStream class. We then move the stream position to the beginning using the Seek method and read the string from the memory stream using the readUnicode method. The string is then printed on the console. Package library: The MemoryStream class is a part of the System.IO namespace, which is included in the .NET Framework package library provided by Microsoft.