Return error: not all code paths return a value
<p>I am trying to develop a method which will check a users input, and <strong>only</strong> return the input, if it passes validation. </p>

<p>this is what I want to do:</p>

<ol>
<li>User enters input</li>
<li>Check value of input <br/></li>
<li>If input satisfies logic, then return that value, else call the function again. </li>
</ol>

<p>this is <em>really</em> what I want, but the complier states that <code>not all code paths return a value</code>:</p>

<pre><code>   public static int UserInput(){
   int input =  int.Parse(Console.ReadLine());
   if (input &lt; 1 || input &gt; 4){
       Console.Write("Invalid Selection. Enter a valid Number (1,2,3 or 4): ");
       if (input &lt; 1 || input &gt; 4)  UserInput();

   } else{
       return input; 
   }
}
</code></pre>

<p>However, this is the following code that satisfies the complier.</p>

<pre><code>    public static int UserInput()
    {
       int input =  int.Parse(Console.ReadLine());
       if (input &lt; 1 || input &gt; 4)
       {
           Console.Write("Invalid Selection. Enter a valid Number (1,2,3 or 4): ");

           if (input &lt; 1 || input &gt; 4)
           {
               UserInput();
               return -1; // Never reached, but must be put in to satisfy syntax of C#
           }
           return input; // Never reached, but must be put in to satisfy syntax of C#
       }
       else
       {
           return input;

       }
    }
</code></pre>

<p>This kind of works, but I get strange results. If a user was to enter in an <code>input</code> that is either 1,2,3 or 4 in the first go (i.e <code>if</code> statement evaluates to <code>false</code>), then the returned input is whatever the user entered. However, if the user was to enter a value that was <em>not</em> 1,2,3 or 4 <em>then</em> enter a valid number, then program would do the following:</p>

<ol>
<li>return input;</li>
<li>jump into the child if statement and run UserInput();</li>
<li>then return -1. </li>
</ol>
