title: How to break a for loop in one function based off of another function
body: <p>I have a method called <code>RunGame</code> that contains an array of tiles. I then have a <code>foreach</code> loop that calls another method called <code>CheckValid</code> that passes in the tile number and a game number. When the checkvalid is called, it checks a field in the database to see if it is null or not. What I would like to do is make the foreach statement stop running if it finds a field that is not null. This is what I currently have:</p>
  <pre><code>public void RunGame()
  {
      try
      {
          int[,] game = new int[10, 1] { { 70 }, { 71 }, { 62 }, { 14 }, { 71 }, { 12 }, { 12 }, { 41 }, { 19 }, { 71 } };
          int num = 2;
  
          foreach (int i in game)
          {
              CheckValid(i, num);
          }
          Console.ReadLine();
      }
      catch (Exception error)
      {
          Console.WriteLine(error);
          Console.ReadLine();
      }
  }
  
  public void CheckValid(int tile, int game)
  {
      using (MySqlCommand cmd = new MySqlCommand(&quot;CheckValid&quot;, Conn))
      {
          cmd.CommandType = CommandType.StoredProcedure;
          cmd.Parameters.AddWithValue(&quot;tileVal&quot;, tile);
          cmd.Parameters.AddWithValue(&quot;num&quot;, game);
          Conn.Open();
          
          using (MySqlDataReader read = cmd.ExecuteReader())
          {
              while (read.Read())
              {
                  if (read[0] != DBNull.Value)
                  {
                      Console.WriteLine($&quot;This tile is occupied&quot;);
                  }
                  else
                  {
                      Console.WriteLine($&quot;Tile {tile} is not occupied&quot;);
                  }
              }
              read.Close();
          }
          Conn.Close();
      }
  }
  </code></pre>
  <p>I would like it to run based on the conditions of this if statement:</p>
  <pre><code>if (read[0] != DBNull.Value)
  {
      Console.WriteLine($&quot;This tile is occupied&quot;);
      //Stop the foreach loop
  }
  else
  {
      Console.WriteLine($&quot;Tile {tile} is not occupied&quot;);
      //Continue the foreach loop
  }
  </code></pre>
  <p>Currently, it prints this when tile 71 is occupied:</p>
  <pre><code>Tile 70 is not occupied
  This tile is occupied //I would like it to stop before it prints this line
  Tile 62 is not occupied
  Tile 14 is not occupied
  This tile is occupied
  Tile 12 is not occupied
  Tile 12 is not occupied
  Tile 41 is not occupied
  Tile 19 is not occupied
  This tile is occupied
  </code></pre>
  <p>Is it possible to achieve this?</p>