Search code examples
c#stringstringreader

StringReader.ReadLine clears the given string, is there chance to not clean the string?


When I am using StringReader.ReadLine to read text from array of strings, it reads it perfectly but it also clears my array,

using (StringReader reader = new StringReader(filesGroupList[x]))
       {
          while ((filesGroupList[x] = reader.ReadLine()) != null)
             {
                ...
             }
        }

Now, filesGroupList is empty. So if i will want to read data from this string again it will give me null reference exception, so the only way for me is to create a copy of this array before using ReadLine, but is there chance to avoid it? so when StringReaded finished reading the line, my line still stays inside array.


Solution

  • Given the code that you have written, here's an example of what I see it doing:

    //going to set filesGroupList[x] to a string and then see what happens.
    filesGroupList[x] = "First line of string.\nSecond line of string.\n";
    //now we go into the using portion.
    using (StringReader reader = new StringReader(filesGroupList[x]))
    {
      //reader has access to the whole string.
      while ((filesGroupList[x] = reader.ReadLine()) != null)
      {
        //first time through, filesGroupList[x] is set to "First line of string."
        //second time through, filesGroupLIst[x] is set to "Second line of string."
        Console.WriteLine(filesGroupList[x]);
      }
      //third time through, ReadLine() returns null.
      //filesGroupList[x] is set to null.
      //Code breaks out of while loop.
      Console.WriteLine(filesGroupList[x]); //outputs an empty line.
    }
    //outside of using, filesGroupList[x] still null.
    Console.WriteLine(filesGroupList[x]); //also outputs an empty line.
    

    Now, given my other answer where I suggest using line, we'll keep everything the same except for the line portion.

    //going to set filesGroupList[x] to a string and then see what happens.
    filesGroupList[x] = "First line of string.\nSecond line of string.\n";
    using (StringReader reader = new StringReader(filesGroupList[x]))
    {
      //reader has access to the whole string.
      string line;
      while ((line = reader.ReadLine()) != null)
      {
        //first time through, line is set to "First line of string."
        //second time through, line is set to "Second line of string."
        Console.WriteLine(line);
      }
      //third time through, ReadLine() returns null.
      //line is set to null.
      //filesGroupList[x] is still set to "First line of string.\nSecond line of string.\n"
      Console.WriteLine(line); //outputs an empty line.
      Console.Write(filesGroupList[x]); //outputs whole string (on 2 lines).
    }
    Console.WriteLine(line); //won't compile. line doesn't exist.
    Console.Write(filesGroupList[x]); //outputs whole string (on 2 lines).
    

    So I don't think you want to read from filesGroupList[x] and then store that in filesGroupList[x]. If the string in filesGroupList[x] has no end of line characters, you're simply putting that string right back into it (and then putting null in your next time through the while loop). If the string in filesGroupList[x] does have end of line characters, then each time through the while loop, you are putting part of the string back into filesGroupList[x], which I don't think is your intention.