既然你所編寫的代碼,這裏是什麼,我看不出它的例子:
//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.
現在,給我其他的答案,我建議使用線,我們就會把一切都相同,除了線部分。
//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]
。如果filesGroupList[x]
字符串沒有行尾字符,你乾脆把這個字符串右後衛它(然後把null
在你下一次通過while
循環)。如果filesGroupList[x]
中的字符串確實有行尾字符,則每次通過while
循環時,都會將字符串的一部分放回filesGroupList[x]
,我不認爲這是您的意圖。
我不明白什麼是你想acheive,它看起來非常奇怪 – nothrow 2013-03-08 16:46:36
我想讀我的字符串具體的信息,但我會稍後再使用此字符串。 – inside 2013-03-08 16:49:35