using System; using System.IO; public class Program { public static void Main() { string str = "Hello, world!"; StringReader reader = new StringReader(str); // Peek at the next character int peekChar = reader.Peek(); Console.WriteLine((char)peekChar); // Prints "H" } }
using System; using System.IO; public class Program { public static void Main() { string str = "Hello, world!"; StringReader reader = new StringReader(str); // Peek at the next character int peekChar = reader.Peek(); while (peekChar != -1) { Console.WriteLine((char)peekChar); reader.Read(); peekChar = reader.Peek(); } } }In this example, we create a new StringReader object that reads from the string "Hello, world!". We use the Peek method in a loop to print out each character in the string one at a time. We use the Read method to advance the current position of the StringReader after each character is printed. The loop exits when Peek returns -1, indicating that there are no more characters to read. These examples use the System.IO namespace.