'Getting "not all code paths return a value" for code that will never not return a value

The method I write doesn't return a value though I write every possible conditions to return a value.

What I understand is whenever the compiler sees the return keyword it'll stop, execute the program and return the value next to the return keyword. Even if it is in the loop it'll break the loop and return the value but it the compiler shows the "not all code paths return a value" error. And there's an else conditions for the other possible conditions and how comes the error show up.

So, how the return keyword actually work?

I am so confused.

using System;

public static class PhoneNumber
{
  public static (bool IsNewYork, bool IsFake, string LocalNumber) Analyze(string phoneNumber)
  {
     bool flag = true;
     string[] numSub = phoneNumber.Split("-");
     while(flag)
        {
            if(numSub[0] == "212")
            {
                if(numSub[1] == "555")
                {
                    return (true, true, numSub[2]);
                }
                else{return (true, false, numSub[2]);}
            } // end of if condition

            else{
                if(numSub[1] == "555")
                    {
                        return (false, true, numSub[2]);
                    }
                else{return (false, false, numSub[2]);}
                } // end of the else condition
         } // end of the loop
    
    }


Solution 1:[1]

not all code paths return a value

The compiler is not "smart" enough to know that you will enter the while loop. So it sees the code path that doesn't enter the while loop as a possible code path without a return.

As-written the code structure doesn't make much sense, so it should probably be restructured to make the compiler happy, and be easier to read and maintain. You can also just a thrown exception after the while to get rid of the compilation error.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1