如何在给定状态下停止foreach循环

时间:2012-10-22 05:58:41

标签: c#

foreach (String host   in hostArray)
{
    string s1 = hostArray[t];
    string s2 = branchArray[t];
    string con_msg;
    t=t+1;
    TcpClient socketForServer;
    try
    {
        socketForServer = new TcpClient(s1, 10);
        con_msg="Connected";
    }
    catch
    {
        ListViewItem item = new ListViewItem();
        item.BackColor = Color.LightGreen;
        item.Text = (s2);
        item.SubItems.Add(s1);
        item.SubItems.Add("Not found");
        listView1.Items.Add(item);
        con_msg = "Not connected";
        // I want to exit this round in here and and start next round  how to do it 
    }

    NetworkStream networkStream = socketForServer.GetStream();
    System.IO.StreamReader streamReader =  new System.IO.StreamReader(networkStream);
    System.IO.StreamWriter streamWriter =new System.IO.StreamWriter(networkStream);
    //code
}

我想退出当前回合并开始下一轮。怎么做。我不想退出foreach循环,我只想退出当前轮次并开始下一轮

4 个答案:

答案 0 :(得分:3)

使用continue来做到这一点。见Documentation

答案 1 :(得分:2)

continue 语句将控制权传递给它出现的封闭迭代语句的下一次迭代。

以下是 continue 语句在您的代码中的样子:

foreach (String host   in hostArray)
{
    string s1 = hostArray[t];
    string s2 = branchArray[t];
    string con_msg;
    t=t+1;
    TcpClient socketForServer;
    try
    {
        socketForServer = new TcpClient(s1, 10);
        con_msg="Connected";
    }
    catch
    {
        ListViewItem item = new ListViewItem();
        item.BackColor = Color.LightGreen;
        item.Text = (s2);
        item.SubItems.Add(s1);
        item.SubItems.Add("Not found");
        listView1.Items.Add(item);
        con_msg = "Not connected";

        continue;
    }

    NetworkStream networkStream = socketForServer.GetStream();
    System.IO.StreamReader streamReader =  new System.IO.StreamReader(networkStream);
    System.IO.StreamWriter streamWriter =new System.IO.StreamWriter(networkStream);
    //code
}

goto 语句将程序控件直接传输到带标签的语句。

以下是 goto 语句在您的代码中的样子:

foreach (String host   in hostArray)
{
    string s1 = hostArray[t];
    string s2 = branchArray[t];
    string con_msg;
    t=t+1;
    TcpClient socketForServer;
    try
    {
        socketForServer = new TcpClient(s1, 10);
        con_msg="Connected";
    }
    catch
    {
        ListViewItem item = new ListViewItem();
        item.BackColor = Color.LightGreen;
        item.Text = (s2);
        item.SubItems.Add(s1);
        item.SubItems.Add("Not found");
        listView1.Items.Add(item);
        con_msg = "Not connected";

        goto ErrorOccured;
    }

    NetworkStream networkStream = socketForServer.GetStream();
    System.IO.StreamReader streamReader =  new System.IO.StreamReader(networkStream);
    System.IO.StreamWriter streamWriter =new System.IO.StreamWriter(networkStream);
    //code

    ErrorOccured:
    Console.WriteLine("Do something else...");
}

答案 2 :(得分:2)

foreach (var item in Items)
{           
  try
  {
    //Exception
  }
  catch (Exception)
  {
    continue;//Will move to next item of "Items" of FOREACH LOOP
  }

  int a = 1 + 1;//If exception gets this line will not execute
 }

答案 3 :(得分:2)

在完成捕获之前使用continue退出当前回合... 通过在catch中使用continue语句,应该跳过continue和foreach主体结尾之间的语句。例如......就像HABJAN给的那样..

    foreach (String host   in hostArray){

        string s1 = hostArray[t];
        string s2 = branchArray[t];
        string con_msg;
        t=t+1;
        TcpClient socketForServer;

       try{
           socketForServer = new TcpClient(s1, 10);
           con_msg="Connected";
       }
       catch{
           ListViewItem item = new ListViewItem();
           item.BackColor = Color.LightGreen;
           item.Text = (s2);
           item.SubItems.Add(s1);
           item.SubItems.Add("Not found");
           listView1.Items.Add(item);
           con_msg = "Not connected";

           continue;
       }

       NetworkStream networkStream = socketForServer.GetStream();
       System.IO.StreamReader streamReader =  new System.IO.StreamReader(networkStream);
       System.IO.StreamWriter streamWriter =new System.IO.StreamWriter(networkStream);
       // code
   }