StringWriter sw = new StringWriter(); sw.Write("Hello World"); // Write something to StringWriter sw.Close(); // Close the StringWriter
using System; using System.IO; class Program { static void Main() { StringWriter sw = new StringWriter(); sw.WriteLine("This is an example of using StringWriter with other classes."); // Create a new StringReader to read the string written by our StringWriter StringReader sr = new StringReader(sw.ToString()); // Print out the contents of the StringReader Console.WriteLine(sr.ReadToEnd()); // Close both the StringWriter and the StringReader sw.Close(); sr.Close(); } }In this example, we use StringWriter to write a string and then use StringReader to read it. Finally, we close both the StringWriter and the StringReader. Package/library: System.IO.