Old*_*dar 2 c# break while-loop
我在Microsoft Visual Studio 2010中使用C#进行编程.在我的代码中的某处,我在检查行的值是否为null之后在while循环中使用break关键字.
我确信行值为null但仍然是break命令不起作用.我怎样才能解决这个问题?
while (!myfile.EndOfStream)
{
string line = myfile.ReadLine();
if (line == null) { break; }
else
{ ...}
}
Run Code Online (Sandbox Code Playgroud)
它不为空,你得到一个空字符串或带有空格的字符串(换行符等).如果您将支票修改为:
if (string.IsNullOrWhiteSpace(line)) { break; } //checks null and white space
// or empty string
Run Code Online (Sandbox Code Playgroud)
String.IsNullOrWhiteSpace随.Net framework 4.0或更高版本提供.