鉴于您编写的代码,这是我看到的一个示例:
//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.
现在,鉴于我建议使用 line 的其他答案,我们将保持所有内容相同,除了 line 部分。
//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).
因此,我认为您不想读取filesGroupList[x]
然后将其存储在filesGroupList[x]
. 如果字符串 infilesGroupList[x]
没有行尾字符,则只需将该字符串放回其中(然后null
在下一次while
循环中放入)。如果 in 中的字符串filesGroupList[x]
确实有行尾字符,那么每次通过while
循环时,您都会将部分字符串放回filesGroupList[x]
,我认为这不是您的意图。