PushbackTextReader reader = new PushbackTextReader(new StringReader("hello world")); int c = reader.Read(); // reads 'h' reader.Unread(c); // pushes 'h' back into the stream c = reader.Read(); // reads 'h' again
PushbackTextReader reader = new PushbackTextReader(new StringReader("1234")); int first = reader.Read(); // reads '1' int second = reader.Read(); // reads '2' reader.Unread(second); // push '2' back into the stream second = reader.Read(); // reads '2' again int third = reader.Read(); // reads '3'In this example, we read the first and second characters from the stream ('1' and '2', respectively) and then push the second character back into the input buffer using the `Unread` method. We then read from the stream again, which results in '2' being returned again. Finally, we read the third character from the stream ('3'). As can be seen in these examples, the `PushbackTextReader` class can be useful in certain parsing scenarios where the parser needs to peek ahead at the next few characters in the input stream to determine the input structure. By allowing characters to be pushed back into the input stream, this class enables efficient and flexible parsing of character data in C#.