title: C# How to loop user input until the datatype of the input is correct?
body: <p>How to make this piece of code loop asking for input from the user until <em>int.TryParse()</em></p>
  
  <p>is successful?</p>
  
  <pre><code>//setX
      public void setX()
      {
          //take the input from the user
          string temp;
          int temp2;
          System.Console.WriteLine("Enter a value for X:");
          temp = System.Console.ReadLine();
          if (int.TryParse(temp, out temp2))
              x = temp2;
          else
              System.Console.WriteLine("You must enter an integer type value"); 'need to make it ask user for another input if first one was of invalid type'
      }
  </code></pre>
  
  <p>Version of the code after the helpful answer:</p>
  
  <pre><code> //setX
      public void setX()
      {
          //take the input from the user
          string temp;
          int temp2;
          System.Console.WriteLine("Enter a value for X:");
          temp = System.Console.ReadLine();
          if (int.TryParse(temp, out temp2))
              x = temp2;
          else
          {
              Console.WriteLine("The value must be of integer type");
              while (!int.TryParse(Console.ReadLine(), out temp2))
                  Console.WriteLine("The value must be of integer type");
              x = temp2;
          }
      }
  </code></pre>