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

  1. <small id='KOAoj'></small><noframes id='KOAoj'>

    • <bdo id='KOAoj'></bdo><ul id='KOAoj'></ul>
  2. <legend id='KOAoj'><style id='KOAoj'><dir id='KOAoj'><q id='KOAoj'></q></dir></style></legend><tfoot id='KOAoj'></tfoot>

      Func vs. Action vs. Predicate

      Func vs. Action vs. Predicate(Func vs. Action vs. Predicate)
    1. <legend id='sOn06'><style id='sOn06'><dir id='sOn06'><q id='sOn06'></q></dir></style></legend>

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

            • <small id='sOn06'></small><noframes id='sOn06'>

              • <bdo id='sOn06'></bdo><ul id='sOn06'></ul>
                <tfoot id='sOn06'></tfoot>

                本文介绍了Func vs. Action vs. Predicate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                有真实的例子和他们的使用,有人可以帮助我理解:

                With real examples and their use, can someone please help me understand:

                1. 我们什么时候需要 Func 委托?
                2. 我们什么时候需要 Action<T>委托?
                3. 我们什么时候需要 谓词<T>委托?

                推荐答案

                FuncAction 的区别只是你是否希望委托返回一个值(使用Func) 与否(使用 Action).

                The difference between Func and Action is simply whether you want the delegate to return a value (use Func) or not (use Action).

                Func 可能是 LINQ 中最常用的——例如在投影中:

                Func is probably most commonly used in LINQ - for example in projections:

                 list.Select(x => x.SomeProperty)
                

                或过滤:

                 list.Where(x => x.SomeValue == someOtherValue)
                

                或按键选择:

                 list.Join(otherList, x => x.FirstKey, y => y.SecondKey, ...)
                

                Action 更常用于 List<T>.ForEach 之类的事情:对列表中的每个项目执行给定的操作.我使用它的频率低于 Func,尽管我 确实 有时将无参数版本用于 Control.BeginInvokeDispatcher.BeginInvoke.

                Action is more commonly used for things like List<T>.ForEach: execute the given action for each item in the list. I use this less often than Func, although I do sometimes use the parameterless version for things like Control.BeginInvoke and Dispatcher.BeginInvoke.

                Predicate 实际上只是一个特殊的 Func,在所有 Func 和大部分 之前引入>Action 代表出现了.我怀疑如果我们已经有各种形式的 FuncAction ,那么 Predicate 就不会被引入......尽管它确实赋予委托的使用某种意义,而 FuncAction 用于广泛不同的目的.

                Predicate is just a special cased Func<T, bool> really, introduced before all of the Func and most of the Action delegates came along. I suspect that if we'd already had Func and Action in their various guises, Predicate wouldn't have been introduced... although it does impart a certain meaning to the use of the delegate, whereas Func and Action are used for widely disparate purposes.

                Predicate 主要用于 List<T> 中,用于 FindAllRemoveAll 等方法.

                Predicate is mostly used in List<T> for methods like FindAll and RemoveAll.

                这篇关于Func vs. Action vs. Predicate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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() 和泛型:错误绑定到目标方法)
                    <tbody id='zJqKN'></tbody>
                    <bdo id='zJqKN'></bdo><ul id='zJqKN'></ul>

                    1. <small id='zJqKN'></small><noframes id='zJqKN'>

                    2. <i id='zJqKN'><tr id='zJqKN'><dt id='zJqKN'><q id='zJqKN'><span id='zJqKN'><b id='zJqKN'><form id='zJqKN'><ins id='zJqKN'></ins><ul id='zJqKN'></ul><sub id='zJqKN'></sub></form><legend id='zJqKN'></legend><bdo id='zJqKN'><pre id='zJqKN'><center id='zJqKN'></center></pre></bdo></b><th id='zJqKN'></th></span></q></dt></tr></i><div id='zJqKN'><tfoot id='zJqKN'></tfoot><dl id='zJqKN'><fieldset id='zJqKN'></fieldset></dl></div>
                    3. <tfoot id='zJqKN'></tfoot>
                      • <legend id='zJqKN'><style id='zJqKN'><dir id='zJqKN'><q id='zJqKN'></q></dir></style></legend>