c# 编译错误 ?

来源:百度知道 编辑:UC知道 时间:2024/04/30 05:38:20
static void Main(string[] args)
{
StreamReader objReader = new StreamReader("lijun.txt");
string sLine = "";

while (sLine != null)
{
sLine = objReader.ReadLine();

string[] split = sLine.Split(new Char[] { ' ', ',', '.', ':' });

foreach (string s in split)
{
if (s.Trim() != "")
Console.WriteLine(s);
}
}

objReader.Close();
Console.ReadLine();
}

在没有while 循环的时候没有任何问题,为什么在加上循环后就有问题了阿?

有了死循环,
正确写法:

StreamReader objReader = new StreamReader("lijun.txt");
string sLine = "";
//"" 和null不是同一个意思
while (sLine != "") //这样也可以while(!objReader.EndOfStream)
{
sLine = objReader.ReadLine().Trim();

string[] split = sLine.Split(new Char[] { ' ', ',', '.', ':' });

foreach (string s in split)
{
if (s.Trim() != "")
Console.WriteLine(s);
}
}

objReader.Close();
Console.ReadLine();
}

string sLine = "";
你给的是空字符串~ 并不是 null