C# 如何在给定状态下停止foreach循环
我想退出当前一轮,开始下一轮。怎么做。我不想退出foreach循环,我只想退出当前轮并开始下一轮使用continue来完成。请参见使用继续执行此操作。请参见continue语句将控制权传递给它所在的封闭迭代语句的下一个迭代 下面是continue语句在代码中的外观:C# 如何在给定状态下停止foreach循环,c#,C#,我想退出当前一轮,开始下一轮。怎么做。我不想退出foreach循环,我只想退出当前轮并开始下一轮使用continue来完成。请参见使用继续执行此操作。请参见continue语句将控制权传递给它所在的封闭迭代语句的下一个迭代 下面是continue语句在代码中的外观: foreach (String host in hostArray) { string s1 = hostArray[t]; string s2 = branchArray[t]; string con_
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 (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";
// 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 (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
}
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";
// 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 (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";
// 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 (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
}
使用“继续”在完成捕获之前退出当前回合。。。
通过在catch中使用continue语句,continue和foreach主体末尾之间的语句应该被跳过。例如。。。这就像哈布詹给我的
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
}
使用“继续”在完成捕获之前退出当前回合。。。
通过在catch中使用continue语句,continue和foreach主体末尾之间的语句应该被跳过。例如。。。这就像哈布詹给我的
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
}
在goto示例中,您必须有另一个标签,因为erroroccurrend将在//代码完成时执行。我不确定使用goto是否是最佳选择。也许使用错误标志bool是更好的选择,然后在末尾使用if标志来选择正确的代码路径。@默认情况下,是的,这就是goto语句的要点。。我只提供了两种可能的解决方案。在他的例子中,continue语句更优雅。在goto示例中,您必须有另一个标签,因为erroroccurrend将在//代码完成时执行。我不确定使用goto是否是最佳选择。也许使用错误标志bool是更好的选择,然后在末尾使用if标志来选择正确的代码路径。@默认情况下,是的,这就是goto语句的要点。。我只提供了两种可能的解决方案。就他的情况而言,continue语句更为优雅。