简体   繁体   English

C#…并非所有代码路径都返回值

[英]C#… Not all code paths return a value

I am trying to create a Collection with properties and their respective accessors. 我试图用属性及其各自的访问器创建一个Collection。

Here is my code: 这是我的代码:

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;
        }
    }
}

I'm receiving the error at the "get" point. 我在“获取”点收到错误。 It tells me that not all code paths return a value... What exactly does this mean, and what am I missing? 它告诉我,并非所有代码路径都返回值...这到底是什么意思,我想念的是什么?

Firstly, the reason you're getting that message is that if this is empty, then the code within the foreach block (which is where the required return statement is) would never be executed. 首先,您收到该消息的原因是,如果this消息为空,则永远不会执行foreach块中的代码(这是所需的return语句所在的位置)。

However, your TotalLength() function would always return the length of the first Song , as you're declaring your variable, setting its value, then returning it within the foreach block. 但是,在声明变量,设置其值,然后在foreach块中返回它时, TotalLength()函数将始终返回第一首Song的长度。 Instead, you'd need to do something like this: 相反,您需要执行以下操作:

int totalLength = 0;

foreach(Song s in this)
{
    total += s.LengthInSeconds;
}

return totalLength;

Your PlayedCount function suffers from similar issues (if the collection is empty or contains no elements whose TimesPlayed property is greater than 0, then there would be no way for it to return a value), so judging by your comment you could write it this way: 您的PlayedCount函数会遇到类似的问题(如果集合为空或不包含TimesPlayed属性大于0的元素,则它将无法返回值),因此,根据您的评论判断,您可以这样编写:

public int PlayedCount()
{
    int total = 0;

    foreach(Song s in this)
    {
        if (s.TimesPlayed > 0)
        {
            total++;
        }
    }

    return total;
}

It means just as it says, not all code paths return a value. 就是说,并非所有代码路径都返回值。

In this case, if your list is empty, then it cannot call return. 在这种情况下,如果您的列表为空,则无法调用return。 In a foreach, there must be at least one item for the code to execute. 在foreach中,必须至少有一项要执行的代码。 Now, maybe you know that the list will always contain a value, but the compiler can't know that 现在,也许您知道列表将始终包含一个值,但是编译器无法知道

What would your method return if this did not evaluate? 如果没有评估,您的方法将返回什么?

 if (s.TimesPlayed > 0)
                {
                    return s.ToString();
                }

try using an else to return an empty string or something 尝试使用else返回空字符串或其他内容

The fact that 'this' could have no songs- in that case the loops will not execute at all and there is no implicit return value in C#. “ this”可能没有歌曲的事实-在这种情况下,循环根本不会执行,并且C#中没有隐式的返回值。

Furthermore, your getters don't really make sense unless you only ever had one song in the collection. 此外,除非您在收藏中只听过一首歌,否则您的吸气器并没有真正意义。 You need something more like this: 您需要这样的东西:

public int TotalLength()
{
    get
    {
        int total = 0;
        foreach (Song s in this)
        {
            total += s.LengthInSeconds;
        }
        return total;    
    }
}

Finally, without knowing how you keep track of TimesPlayed for each individual song, I wouldn't know how to implement that getter, but I am sure you can figure it out with this much. 最后,在不知道如何跟踪每首歌曲的TimesPlayed的情况下,我不知道如何实现该getter,但我相信您可以通过这一点弄清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM