C#退出使用lambda的泛型ForEach

发布于 2024-08-21 10:09:40 字数 291 浏览 5 评论 0原文

有谁知道是否可以退出使用 lambda 的通用 ForEach?例如,

someList.ForEach(sl =>
  {
    if (sl.ToString() == "foo")
        break;

    // continue processing sl here
    // some processing code
  }
);

这段代码本身无法编译。我知道我可以使用常规的 foreach,但为了保持一致性,我想使用 lambda。

非常感谢。

Does anyone know if it is possible to exit a generic ForEach that uses lambda? e.g.

someList.ForEach(sl =>
  {
    if (sl.ToString() == "foo")
        break;

    // continue processing sl here
    // some processing code
  }
);

This code itself won't compile. I know I could use a regular foreach but for consistency I want to use lambda.

Many thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

表情可笑 2024-08-28 10:09:40

当然。但首先请注意,我建议不要这样做;我说序列运算符不应该有副作用,而语句应该有副作用。如果您要在 ForEach lambda 中执行某些操作,请将其作为 foreach 循环体中的语句,而不是使其看起来像序列运算符。

也就是说,这就是你要做的。首先,您自己编写一个适用于任意序列而不仅仅是列表的 ForEach:

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

现在您可以像这样编写中断:

someList
    .TakeWhile(x=>x.ToString() != "foo")
    .ForEach(sl=>
    {/*your action here*/});

Sure. But first, note that I recommend against this; I say that a sequence operator should not have a side effect, and a statement should have a side effect. If you're doing something in that ForEach lambda, then make it a statement in the body of a foreach loop rather than making it look like a sequence operator.

That said, here's what you do. First, you write yourself a ForEach that works on arbitrary sequences, not just lists:

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

And now you write your break like this:

someList
    .TakeWhile(x=>x.ToString() != "foo")
    .ForEach(sl=>
    {/*your action here*/});
方圜几里 2024-08-28 10:09:40

来自 MSDN

以下规则适用于变量
lambda 表达式中的范围:

截图

lambda 表达式不能包含
goto 语句、break 语句或
continue 语句,其目标是
在体外或体内
包含匿名函数。

不知道您发布的代码是否有帮助。相关引用来自MSDN文章末尾。

From MSDN

The following rules apply to variable
scope in lambda expressions:

snip

A lambda expression cannot contain a
goto statement, break statement, or
continue statement whose target is
outside the body or in the body of a
contained anonymous function.

Don't know if that helps given the code you posted. The relevant quote is from the end of the MSDN article.

失而复得 2024-08-28 10:09:40

警告:以下代码仅供娱乐,请勿认真对待!

您可以像这样“模拟”提前返回的继续:

Enumerable.Range(1, 20)
          .ForEach(n =>
                {
                    if (n == 10) return;
                    Console.WriteLine("This is not 10: {0}", n);
                });

也就是说,我认为 lambda 内的副作用表明您做错了。请改用适当的 foreach。或者像 TakeWhile 这样的东西,正如 Eric 已经善意地演示的那样。

Warning: the code below is not to be taken seriously and is provided for entertainment purposes only!

You can 'simulate' a continue with an early return like this:

Enumerable.Range(1, 20)
          .ForEach(n =>
                {
                    if (n == 10) return;
                    Console.WriteLine("This is not 10: {0}", n);
                });

That said, I think that side effects within lambda's are a sign that you're doing it wrong. Use a proper foreach instead. Or something like TakeWhile, as Eric kindly demonstrated already.

〃温暖了心ぐ 2024-08-28 10:09:40

这个怎么样?

        Enumerable.Range(1, 10)
        .Where(x => x % 2 != 0)
        .ToList()
        .ForEach(x => Console.WriteLine(x));

How about this?

        Enumerable.Range(1, 10)
        .Where(x => x % 2 != 0)
        .ToList()
        .ForEach(x => Console.WriteLine(x));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文