我想创建一个具有属性和它们各自的访问器的集合。C#...不是所有的代码路径返回值
这里是我的代码:
class SongCollection : List<Song>
{
private string playedCount;
private int totalLength;
public string PlayedCount
{
get
{
foreach (Song s in this)
{
if (s.TimesPlayed > 0)
{
return s.ToString();
}
}
}
}
public int TotalLength
{
get
{
foreach (Song s in this)
{
int total = 0;
total += s.LengthInSeconds;
}
return total;
}
}
}
我收到错误的“获取”点。它告诉我,并不是所有的代码路径都返回一个值......这意味着什么,我错过了什么?
我认为你需要检讨你的设计。特别是你的'PlayedCount'属性。 – ChaosPandion
您忘记返回if语句 – Holystream