Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 循环最佳实践_C#_Break_Continue - Fatal编程技术网

C# 循环最佳实践

C# 循环最佳实践,c#,break,continue,C#,Break,Continue,我有一个非常大的循环,循环1000行。如果找到魔法值1,我将退出循环。如果未找到魔法值1,但找到魔法值2,则循环需要跳到开头。现在我正在使用一个开关,一些ifs和一个goto。我已经读到goto不是最好的方式。有没有更好的方法来实现这一点?要退出循环,可以使用语句;要进入下一条记录,可以使用语句 for(int i = 0; i < 1000; i++) { if(magicValue1) break; if(magicValue2) conti

我有一个非常大的循环,循环1000行。如果找到魔法值1,我将退出循环。如果未找到魔法值1,但找到魔法值2,则循环需要跳到开头。现在我正在使用一个开关,一些ifs和一个goto。我已经读到goto不是最好的方式。有没有更好的方法来实现这一点?

要退出循环,可以使用语句;要进入下一条记录,可以使用语句

for(int i = 0; i < 1000; i++)
{
    if(magicValue1)
       break;
    if(magicValue2)
       continue;
}
for(int i=0;i<1000;i++)
{
如果(magicValue1)
打破
如果(magicValue2)
继续;
}
我不是纵容使用GOTO语句,我只是指出一个可能的用例

您可以使用goto跳转语句来启动/退出循环,但是除非您使用嵌套循环,否则我不会使用此选项。我认为goto语句仍然可以用于优化、干净地退出等。。但总的来说,最好少用它

for(int i = 0; i < 100; i++)
{ 
  start:

  for(int i = 0; i < 10; i++)
  {
     if(magicValue1)
       goto end;
    if(magicValue2)
       goto start;
  }
}
end : 
for(int i=0;i<100;i++)
{ 
开始:
对于(int i=0;i<10;i++)
{
如果(magicValue1)
转到终点;
如果(magicValue2)
转到开始;
}
}
完:

要退出循环,可以使用语句;要进入下一条记录,可以使用语句

for(int i = 0; i < 1000; i++)
{
    if(magicValue1)
       break;
    if(magicValue2)
       continue;
}
for(int i=0;i<1000;i++)
{
如果(magicValue1)
打破
如果(magicValue2)
继续;
}
我不是纵容使用GOTO语句,我只是指出一个可能的用例

您可以使用goto跳转语句来启动/退出循环,但是除非您使用嵌套循环,否则我不会使用此选项。我认为goto语句仍然可以用于优化、干净地退出等。。但总的来说,最好少用它

for(int i = 0; i < 100; i++)
{ 
  start:

  for(int i = 0; i < 10; i++)
  {
     if(magicValue1)
       goto end;
    if(magicValue2)
       goto start;
  }
}
end : 
for(int i=0;i<100;i++)
{ 
开始:
对于(int i=0;i<10;i++)
{
如果(magicValue1)
转到终点;
如果(magicValue2)
转到开始;
}
}
完:
这个怎么样:

for(int i = 0; i < 1000; i++) {
    if(values[i] == MAGIC_VALUE_1) {
        break;
    } else if(values[i] == MAGIC_VALUE_2) {
        i = 0;
    }
}
for(int i=0;i<1000;i++){
如果(值[i]==魔法值\u 1){
打破
}else if(值[i]==魔法值\u 2){
i=0;
}
}
如果“跳到开头”是指“跳过此记录并处理下一条记录”,请将
i=0
替换为
继续

如何:

for(int i = 0; i < 1000; i++) {
    if(values[i] == MAGIC_VALUE_1) {
        break;
    } else if(values[i] == MAGIC_VALUE_2) {
        i = 0;
    }
}
for(int i=0;i<1000;i++){
如果(值[i]==魔法值\u 1){
打破
}else if(值[i]==魔法值\u 2){
i=0;
}
}

如果“跳到开头”的意思是“跳过此记录并处理下一条记录”,则将
i=0
替换为
continue
A
,而将
变量替换为no
break

bool continue = true; int i = 0;
while (i < 1000 && continue){
    if(values[i] == MAGIC_VALUE_1) {
        continue=false;
    } else if(values[i] == MAGIC_VALUE_2) {
        i = 0;
    }
    i++;
}
bool continue=true;int i=0;
而(i<1000&&continue){
如果(值[i]==魔法值\u 1){
continue=false;
}else if(值[i]==魔法值\u 2){
i=0;
}
i++;
}

A
变化无
中断

bool continue = true; int i = 0;
while (i < 1000 && continue){
    if(values[i] == MAGIC_VALUE_1) {
        continue=false;
    } else if(values[i] == MAGIC_VALUE_2) {
        i = 0;
    }
    i++;
}
bool continue=true;int i=0;
而(i<1000&&continue){
如果(值[i]==魔法值\u 1){
continue=false;
}else if(值[i]==魔法值\u 2){
i=0;
}
i++;
}
我采用#2的情况是指您不希望执行(即跳过)第#2情况中的循环体,而不是希望将循环重置为0。(如果我有反向操作,请参阅代码注释。)

这个建议可能会引起争议,因为for循环中不太传统的条件可以说在自我记录的尺度上很低,但是如果这不困扰你,那么我认为你想要的一种简洁的写作方式是:

        for (int i= 0; i<values.Length && values[i]!= MAGIC_1; i++)
        {
            if (values[i] == MAGIC_2)
            {
                // Don't do the loop body for this case but continue on looping
                continue;
                // If you want to reset the loop to zero instead of skip the 2 case,
                // comment-out the continue; and un-comment the line below:
                // i=0;
            }
            // Do long loop body here
        }
for(int i=0;i我采用#2的情况是指您不想在#2的情况下执行(即跳过)循环体,而不是想将循环重置为0。(如果我有反向操作,请参阅代码注释。)

这个建议可能会引起争议,因为for循环中不太传统的条件可以说在自我记录的尺度上很低,但是如果这不困扰你,那么我认为你想要的一种简洁的写作方式是:

        for (int i= 0; i<values.Length && values[i]!= MAGIC_1; i++)
        {
            if (values[i] == MAGIC_2)
            {
                // Don't do the loop body for this case but continue on looping
                continue;
                // If you want to reset the loop to zero instead of skip the 2 case,
                // comment-out the continue; and un-comment the line below:
                // i=0;
            }
            // Do long loop body here
        }
for(int i=0;i我还不能评论(1个代表点)

但这不是更好吗:

for (int i = 0; i < 1000; i++)
{
    if (magicValue1)
    {
       break;
    }
    else if (magicValue2)
    {
       dosomething();
       i=0;
    }
}
for(int i=0;i<1000;i++)
{
如果(magicValue1)
{
打破
}
else if(magicValue2)
{
dosomething();
i=0;
}
}
我不确定“重新启动搜索”是什么意思。

我还不能评论(1个代表点)

但这不是更好吗:

for (int i = 0; i < 1000; i++)
{
    if (magicValue1)
    {
       break;
    }
    else if (magicValue2)
    {
       dosomething();
       i=0;
    }
}
for(int i=0;i<1000;i++)
{
如果(magicValue1)
{
打破
}
else if(magicValue2)
{
dosomething();
i=0;
}
}

我不知道“重新开始搜索”是什么意思.

请注意,如果您将计数器设置回0(如果MagicValue为2),并且您的代码从未更改值,那么您可能将处于无限循环中。

请注意,如果您将计数器设置回0(如果MagicValue为2),并且您的代码从未更改值,那么您可能将处于无限循环中。

更复杂的是x可以是:

我们定义了两种扩展方法

public static class Extensions
{
   public static bool isMagic_1(this int i)
   {
         return i == 1;
   }

   public static bool isMagic_2(this int i)
   {
         return i == 2;
   }
}
现在您可以执行以下操作:

  for(int i = 0; i < 1000; i++)
  {
     if(i.isMagic_1())
       break;
     if(i.isMagic_2())
       continue;
  }
for(int i=0;i<1000;i++)
{
if(i.isMagic_1())
打破
if(i.isMagic_2())
继续;
}

希望这有帮助!

更复杂的可能是:

我们定义了两种扩展方法

public static class Extensions
{
   public static bool isMagic_1(this int i)
   {
         return i == 1;
   }

   public static bool isMagic_2(this int i)
   {
         return i == 2;
   }
}
现在您可以执行以下操作:

  for(int i = 0; i < 1000; i++)
  {
     if(i.isMagic_1())
       break;
     if(i.isMagic_2())
       continue;
  }
for(int i=0;i<1000;i++)
{
if(i.isMagic_1())
打破
if(i.isMagic_2())
继续;
}

希望这有帮助!

请发布一些代码。这将帮助我们可视化和改进它。通过“跳到开头”,您的意思是在下一次迭代时重新启动循环,还是在第一次迭代时重新启动循环?如果您给出一些示例代码和您实际想要实现的内容,这可能会有所帮助。您描述的内容听起来像是什么