简体   繁体   English

如何在扩展方法中使用'continue'和`break`?

[英]How can I use 'continue' and `break` in an extension method?

I have defined the following extension method: 我已经定义了以下扩展方法:

public static void ForEach<T>(this IEnumerable<T> sequence, Action<T> action)
{
   foreach (T obj in sequence)
   { 
      action(obj); 
   } 
}

I can then use it as: 然后我可以用它作为:

new [] {1, 2, 3} // an IEnumerable<T>
.ForEach(n => 
{
  // do something 
});

I want to be able to take advantage of continue and break inside my extension method so that I can do: 我希望能够利用continuebreak我的扩展方法,以便我可以这样做:

new [] {1, 2, 3}
.ForEach(n => 
{
    // this is an overly simplified example
    // the n==1 can be any conditional statement
    // I know in this case I could have just used .Where
    if(n == 1) { continue; }
    if(n == -1) { break; }      
    // do something 
});

Can these keywords only be used within a for , foreach , while or do-while loops? 这些关键字只能在forforeachwhiledo-while循环中使用吗?

Can these keywords only be used within a for, foreach, while loops? 这些关键字只能在for,foreach,while循环中使用吗?

Yes. 是。 Those statements are limited to loop types. 这些语句仅限于循环类型。 As the docs say : 正如文档所说

The continue statement passes control to the next iteration of the enclosing while, do, for, or foreach statement in which it appears. continue语句将控制权传递给封闭的while,do,for或foreach语句的下一次迭代。

And : 而且

The break statement terminates the closest enclosing loop or switch statement in which it appears. break语句终止它出现的最近的封闭循环或switch语句 Control is passed to the statement that follows the terminated statement, if any. 控制权将传递给终止语句后面的语句(如果有)。

I'd recommend you use a regular foreach , which is self expressive as is. 我建议你使用一个常规的foreach ,它是自我表达的。 I think any attempt to use them semantically inside your ForEach extension method will result in weirder code than using a regular loop. 我认为任何在ForEach扩展方法中语义使用它们的尝试都会产生比使用常规循环更奇怪的代码。

I mean, is this not simpler?: 我的意思是,这不简单吗?:

var arr = new [] {1, 2, 3}
foreach (int number in arr)
{
    if(n == 1) { continue; }      
    if(n == -1) { break; }      
}

In addition to Yuval's answer I'd like to add that you could implement something like this as follows: 除了Yuval的回答之外,我还想补充一点,你可以按如下方式实现:

Change the Action<T> into a Func<T, bool> which takes a T parameter and returns a bool result. Action<T>更改为Func<T, bool> ,它接受一个T参数并返回一个bool结果。

The "continue" case could easily be handled by returning from the function upon the condition, thus continuing with the next iteration of the loop. 通过在条件上从函数返回来容易地处理“继续”情况,从而继续循环的下一次迭代。

The "break" case could be handled by returning a bool from the function that indicates whether to continue or not: 可以通过从函数返回bool来处理“break”情况,该函数指示是否继续:

public static void ForEach<T>(this IEnumerable<T> sequence, Func<T, bool> action)
{
  foreach (T obj in sequence)
  { 
    if (!action(obj)) 
      break;
  }
}

new [] {1, 2, 3}.ForEach(n => 
{
    if(n == 1) { return true;}      
    if(n == -1) { return false; }  

    // do something 
    ...
    return true;
});

Yes, those keyword are limited to while , do , for , and foreach (as Yuval referenced). 是的,这些关键字仅限于whiledoforforeach (如Yuval所引用)。

This code would resemble roughly the same as you ask: 此代码与您要求的大致相同:

bool shouldBreak = false;

new [] {1, 2, 3}
.ForEach(n => 
{
    if (!shouldBreak)
    {
        if(n == 1) { /* no action */ }      
        else if(n == -1) { shouldBreak = true; }
        else
        {
            // do something 
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM