虽然语法 [英] While Syntax

查看:77
本文介绍了虽然语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我知道这听起来有些奇怪,但是有人告诉我您可以分配一个值并在while子句中检查null,即

Ok, so I know it sounds a little strange but someone was telling me that you can assign a value AND check for null in a while clause i.e

string line;
StreamReader sr = new StreamReader("some file");
while(line = sr.ReadLine() != null)
{
    // do something with line
}



看起来或听起来不正确,但这是一个不错的捷径:



Doesn''t look or sound right, but it would be a nice short cut over:

string line;
StreamReader sr = new StreamReader("some file");
line = sr.ReadLine();
while(line != null)
{
    // do something with line
    line = sr.ReadLine();
}

推荐答案

关闭,缺少一组括号
Close, missing a set of parentheses
while( (line = sr.ReadLine()) != null)


这篇关于虽然语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆