<tfoot id='KCX3A'></tfoot>

    <small id='KCX3A'></small><noframes id='KCX3A'>

  1. <legend id='KCX3A'><style id='KCX3A'><dir id='KCX3A'><q id='KCX3A'></q></dir></style></legend>
    <i id='KCX3A'><tr id='KCX3A'><dt id='KCX3A'><q id='KCX3A'><span id='KCX3A'><b id='KCX3A'><form id='KCX3A'><ins id='KCX3A'></ins><ul id='KCX3A'></ul><sub id='KCX3A'></sub></form><legend id='KCX3A'></legend><bdo id='KCX3A'><pre id='KCX3A'><center id='KCX3A'></center></pre></bdo></b><th id='KCX3A'></th></span></q></dt></tr></i><div id='KCX3A'><tfoot id='KCX3A'></tfoot><dl id='KCX3A'><fieldset id='KCX3A'></fieldset></dl></div>
      • <bdo id='KCX3A'></bdo><ul id='KCX3A'></ul>
    1. 使用匿名委托时如何退出 List&lt;string&gt;.ForEach 循环?

      How do i exit a Listlt;stringgt;.ForEach loop when using an anonymous delegate?(使用匿名委托时如何退出 Listlt;stringgt;.ForEach 循环?)

        <i id='Y2wUq'><tr id='Y2wUq'><dt id='Y2wUq'><q id='Y2wUq'><span id='Y2wUq'><b id='Y2wUq'><form id='Y2wUq'><ins id='Y2wUq'></ins><ul id='Y2wUq'></ul><sub id='Y2wUq'></sub></form><legend id='Y2wUq'></legend><bdo id='Y2wUq'><pre id='Y2wUq'><center id='Y2wUq'></center></pre></bdo></b><th id='Y2wUq'></th></span></q></dt></tr></i><div id='Y2wUq'><tfoot id='Y2wUq'></tfoot><dl id='Y2wUq'><fieldset id='Y2wUq'></fieldset></dl></div>
        • <tfoot id='Y2wUq'></tfoot>

            <tbody id='Y2wUq'></tbody>
            1. <legend id='Y2wUq'><style id='Y2wUq'><dir id='Y2wUq'><q id='Y2wUq'></q></dir></style></legend>

              <small id='Y2wUq'></small><noframes id='Y2wUq'>

              • <bdo id='Y2wUq'></bdo><ul id='Y2wUq'></ul>
                本文介绍了使用匿名委托时如何退出 List&lt;string&gt;.ForEach 循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                在正常循环中,您可以使用 break 跳出循环.使用匿名委托也可以做到这一点吗?

                In a normal loop you can break out of a loop using break. Can the same be done using an anonymous delegate?

                示例inputString 和 result 都在委托之外声明.

                Example inputString and result are both declared outside the delegate.

                blackList.ForEach(new Action<string>(
                    delegate(string item)
                    {
                        if(inputString.Contains(item)==true)
                        {
                            result = true;
                            // I want to break here
                        }
                    }
                ));
                

                感谢您的回复,我实际上正在阅读您的书 John :) 只是为了记录,我遇到了这个问题并切换回正常的 foreach 循环,但我发布了这个问题,看看我是否遗漏了什么.

                Thanks for the replies, I'm actually reading your book at the minute John :) Just for the record i hit this issue and switched back to a normal foreach loop but I posted this question to see if i missed something.

                推荐答案

                正如其他人所发布的,您无法在 ForEach 中退出循环.

                As others have posted, you can't exit the loop in ForEach.

                你会使用 LINQ 吗?如果是这样,您可以轻松地结合 TakeWhile 和自定义 ForEach 扩展方法(现在几乎每个项目似乎都有).

                Are you able to use LINQ? If so, you could easily combine TakeWhile and a custom ForEach extension method (which just about every project seems to have these days).

                但是,在您的示例中,List<T>.FindIndex 将是最好的选择 - 但如果您实际上并没有这样做,请发布您真正想要做的示例.

                In your example, however, List<T>.FindIndex would be the best alternative - but if you're not actually doing that, please post an example of what you really want to do.

                这篇关于使用匿名委托时如何退出 List&lt;string&gt;.ForEach 循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                How to store delegates in a List(如何将代表存储在列表中)
                How delegates work (in the background)?(代表如何工作(在后台)?)
                C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)

              • <i id='5wMu4'><tr id='5wMu4'><dt id='5wMu4'><q id='5wMu4'><span id='5wMu4'><b id='5wMu4'><form id='5wMu4'><ins id='5wMu4'></ins><ul id='5wMu4'></ul><sub id='5wMu4'></sub></form><legend id='5wMu4'></legend><bdo id='5wMu4'><pre id='5wMu4'><center id='5wMu4'></center></pre></bdo></b><th id='5wMu4'></th></span></q></dt></tr></i><div id='5wMu4'><tfoot id='5wMu4'></tfoot><dl id='5wMu4'><fieldset id='5wMu4'></fieldset></dl></div>

                <small id='5wMu4'></small><noframes id='5wMu4'>

                <legend id='5wMu4'><style id='5wMu4'><dir id='5wMu4'><q id='5wMu4'></q></dir></style></legend>

                • <bdo id='5wMu4'></bdo><ul id='5wMu4'></ul>
                    <tbody id='5wMu4'></tbody>

                        <tfoot id='5wMu4'></tfoot>