0

I have the return statement for both if and else conditions within a foreach loop. Yet, I get this error. However, when I add a return statement after the loop ends, the error is resolved. That's fine! But what is a problem with the code below?

public static bool Function(List<String> list)
{
    foreach (var item in list)
    {
        if(item != "test")
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    //return false
}

2 Answers2

3

If list is empty (i.e. has no items) or is null, then the loop will not run. This means the code cannot reach any of the return statements, because they are all within the loop. In that case, there is no way for the program to know what value to return from the method. There is a "path" through the method which does not return a value.

This is an impossible situation - a non-void method must return something, so the compiler will not allow you to build and run the program until you have resolved it by adding an extra return statement at the end after the loop finishes, to cover the situation I've described.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • I have checked the list for empty. the code below still has got the error. `if(list.Count != 0) { foreach (var item in list) { if (item != "test") { return false; } else { return true; } } } else { return false; } ` – Ishwor Khadka May 10 '20 at 08:56
  • You don't need the if, it's redundant. If the list is empty the loop already won't run. Just add return false after the end of the loop. – ADyson May 10 '20 at 09:08
1

Edited: First of all, I like to send my regards to @ADyson, for spending a lot of his precious time to help people.

Second Part is in your code there is an issue because you just checking the first item of a list, which can be empty in the first place. If you want to be 100% sure there is no item with test value, you should change your if condition part. Why? because I mentioned you just checking the first item in your list, not the rest of the elements.

So I wrote this function with extra checking for the empty lists.

public static bool Function(List<String> list)
{
    if(list.Count == 0)
    {
       // You can return whatever you want false or true
        return true;
    }

    foreach (var item in list)
    {
       if(item != "test")
       {
          return false;
       }
    }
  return true;
}
Ali Kianoor
  • 1,167
  • 9
  • 18
  • 4
    Your answer would be greatly improved by explaining _why_ (in your opinion) it should be done this way. – ADyson May 10 '20 at 08:35
  • I tried to answer his question by fixing the return issue! but I got minus for helping! – Ali Kianoor May 11 '20 at 06:26
  • 1
    Read my first comment again, and notice the number of upvotes that comment received, and perhaps you will understand the reason for that. Answers which are just code without an explanation (or with a minimal text like yours which makes a statement but doesn't properly explain the reasoning behind it) are not actually all that helpful to people. Take a look at my answer to this same question, for comparison. – ADyson May 11 '20 at 10:27
  • I know what you mean, I will answer the next question with more detail, but I think downvote for the right answer is not fair! – Ali Kianoor May 11 '20 at 11:41
  • 1
    Hover your mouse over any answer's downvote button. As you can see the reason for downvoting answers is not whether they are "correct" but whether they are "useful". Consider whether a code-only answer is useful or not. BTW you can always edit this answer if you want to improve it and attract upvotes. I didn't downvote it, but I would consider upvoting an answer which had a clear explanation of why it was necessary to make the changes you have done, and what effect it will have. Don't forget you haven't simply added a return statement, you've also removed another part of the OP's code. – ADyson May 11 '20 at 12:27
  • Alternatively if you prefer not to edit it, you can regain your lost reputation points by simply deleting the answer. The choice is entirely yours. – ADyson May 11 '20 at 12:29
  • I do appreciate your guide @ADyson, I will edit my post to explain why I could solve the problem like that. If you like I want to add you to my friend list in my Linkedin: https://www.linkedin.com/in/askianoor/ – Ali Kianoor May 11 '20 at 14:10
  • I don't use LinkedIn myself, but I appreciate the gesture. Let me know when you have updated the answer. And good luck with future projects and on StackOverflow. – ADyson May 11 '20 at 14:26
  • Thank you, @ADyson – Ali Kianoor May 11 '20 at 17:03
  • No problem. Have an upvote. You know, you could just delete the bit you originally wrote, there is no need to keep it now that you have a better version. If people really want to see what you wrote the first time, they can simply click on the "edited X minutes ago" link in your answer, and view the edit history - it's here: https://stackoverflow.com/posts/61709204/revisions – ADyson May 11 '20 at 17:09
  • Oh my god, I didn't notice that link :) I will do that. – Ali Kianoor May 11 '20 at 17:28