помогите разобраться stringReader.Read()

не могу понять, как реализовать этот метод. TODO. Implement the method by reading the next character with StringReader.Read method. Вот что у меня получилось

public static bool ReadNextCharacter(StringReader stringReader, out char currentChar)
    {            
        currentChar = (char)stringReader.Read();
        bool result = false;

        int c = stringReader.Read();

        if (c != -1)
        {
            result = true;
        }

        return result;
    }

а вот какие тесты ожидаются

    [TestCase(0, 'L', 'o', ExpectedResult = true)]
    [TestCase(1, 'o', 'r', ExpectedResult = true)]
    [TestCase(2, 'r', 'e', ExpectedResult = true)]
    [TestCase(3, 'e', 'm', ExpectedResult = true)]
    [TestCase(4, 'm', ' ', ExpectedResult = true)]
    [TestCase(124, 'U', 't', ExpectedResult = true)]
    [TestCase(444, '.', ' ', ExpectedResult = true)]
    [TestCase(448, ' ', ' ', ExpectedResult = false)]

как я понял задание: у меня есть строка, в ней я нахожу символ. Если его нет - возвращаю -1 и false соответственно. Но я не уверен, что правильно понял задание...


Ответы (1 шт):

Автор решения: aepot

Вы перемудрили. Считывать символ надо 1 раз, а не 2.

public static bool ReadNextCharacter(StringReader stringReader, out char currentChar)
{            
    int code = stringReader.Read();
    if (code == -1)
    {
        currentChar = '\0';
        return false;
    }
        
    currentChar = (char)code;
    return true;
}
→ Ссылка