问个很菜的return问题,请帮忙。

Iveny 2005-12-13 04:29:58
public string rd()
{
System.IO.StreamReader sr = new System.IO.StreamReader("123.dat", System.Text.Encoding.Default);
while (sr.Peek() >= 0)
{
return (sr.ReadLine());
}



编译错误:并非所有的代码路径都返回值
...全文
106 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Lcindep110 2005-12-13
  • 打赏
  • 举报
回复
楼上正解!!!
xvting 2005-12-13
  • 打赏
  • 举报
回复
public StringCollection rd()
{
StringCollection strDat = new StringCollection();
System.IO.StreamReader sr = new System.IO.StreamReader("123.dat", System.Text.Encoding.Default);
while (sr.Peek() >= 0)
{
strDat .add(sr.ReadLine());
}
return strDat;
}
zhy0101 2005-12-13
  • 打赏
  • 举报
回复
return之后函数就返回不再执行了。
纷享刘晨 2005-12-13
  • 打赏
  • 举报
回复
你sr.ReadLine()了一条,就return了,肯定只有一行
Iveny 2005-12-13
  • 打赏
  • 举报
回复
非常谢谢,可是123.dat里有5行文本,现在只显示出了一行,请教!
CityhunterID 2005-12-13
  • 打赏
  • 举报
回复
因为 while 条件并不一定成立。

public string rd()
{
System.IO.StreamReader sr = new System.IO.StreamReader("123.dat", System.Text.Encoding.Default);
while (sr.Peek() >= 0)
{
return (sr.ReadLine());
}
return null;
}

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧