C# StringReader.ReadLine清除给定字符串,是否有可能不清除该字符串?

C# StringReader.ReadLine清除给定字符串,是否有可能不清除该字符串?,c#,string,stringreader,C#,String,Stringreader,当我使用StringReader.ReadLine从字符串数组中读取文本时,它可以完美地读取文本,但也会清除我的数组 using (StringReader reader = new StringReader(filesGroupList[x])) { while ((filesGroupList[x] = reader.ReadLine()) != null) { ... }

当我使用StringReader.ReadLine从字符串数组中读取文本时,它可以完美地读取文本,但也会清除我的数组

using (StringReader reader = new StringReader(filesGroupList[x]))
       {
          while ((filesGroupList[x] = reader.ReadLine()) != null)
             {
                ...
             }
        }
现在,filegrouplist是空的。所以,如果我想再次从这个字符串读取数据,它会给我空引用异常,所以我唯一的方法是在使用ReadLine之前创建这个数组的副本,但是有机会避免吗?因此,当StringRead读取完该行时,我的行仍保留在数组中。

编辑

实际上,在using语句之后,字符串为空,因为:

Dispose:释放TextReader对象使用的所有资源。(从TextReader继承。)

问题是你为什么要这样做,你想做什么似乎不合理。没有更多的上下文信息


你的代码功能完美,或者你无法解释你自己,或者你的问题完全不相关

我只是试着自己:

static void Main(string[] args)
{
    string[] filesGroupList = new string[1];

    int x = 0;

    filesGroupList[x] = "String is here!";

    using (StringReader reader = new StringReader(filesGroupList[x]))
    {
        while ((filesGroupList[x] = reader.ReadLine()) != null)
        {
            Console.WriteLine("the string is here, I just checked");
            Console.WriteLine(filesGroupList[x]);
        }
    }
    Console.ReadLine();
}
以及输出:

字符串在这里,我刚检查过

绳子在这里


我认为你的错误就在这里:

while ((filesGroupList[x] = reader.ReadLine()) != null)
每次读取和上次都在更改
filegrouplist[x]
的值,将其设置为
null

相反,如果您做了如下操作:

string line;
while ((line = reader.ReadLine()) != null)
...

一旦您脱离了使用范围,您会发现
filegrouplist[x]
没有改变。

鉴于您编写的代码,下面是一个示例,我看到它在做什么:

//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).

所以我认为您不想从
filegrouplist[x]
中读取,然后将其存储在
filegrouplist[x]
中。如果
filegrouplist[x]
中的字符串没有行尾字符,您只需将该字符串重新放入其中(然后在下次通过
while
循环将
null
放入)。如果
filegrouplist[x]
中的字符串确实有行尾字符,那么每次通过
while
循环时,您都会将部分字符串放回
filegrouplist[x]
,我想这不是您的意图。

我不明白您想要实现什么,看起来很奇怪,我正在尝试从我的字符串中读取特定信息,但我以后将不得不再次使用此字符串;在使用statement之后,我想我已经解释了为什么要这样做:我以后必须再次使用这个字符串。但是为什么需要
StringReader
?你能不能用foreach循环
文件组列表
。只有从FileGroupList[x]中读取所有行后,它才会为空。