Func vs. Action vs. Predicate [复制]

本文详细解释了Func、Action和Predicate三种委托的区别及其在编程中的应用。Func用于可能需要参数并返回值的函数,常见于LINQ的投影;Action用于不需要返回值的函数,常用于事件处理;Predicate作为Func的一种特例,用于根据条件评估值并返回布尔结果,常用于List<T>的查找和移除。

本文翻译自:Func vs. Action vs. Predicate [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

With real examples and their use, can someone please help me understand: 通过实例和它们的使用,有人可以帮助我理解:

  1. When do we need Func delegate? 我们什么时候需要Func委托?
  2. When do we need Action delegate? 我们什么时候需要行动委托?
  3. When do we need Predicates delegate? 我们什么时候需要Predicates委托?

#1楼

参考:https://stackoom.com/question/I7Al/Func-vs-Action-vs-Predicate-复制


#2楼

Action is a delegate (pointer) to a method, that takes zero, one or more input parameters, but does not return anything. Action是方法的委托(指针),它接受零个,一个或多个输入参数,但不返回任何内容。

Func is a delegate (pointer) to a method, that takes zero, one or more input parameters, and returns a value (or reference). Func是方法的委托(指针),它接受零个,一个或多个输入参数,并返回一个值(或引用)。

Predicate is a special kind of Func often used for comparisons. 谓词是一种特殊的Func,经常用于比较。

Though widely used with Linq, Action and Func are concepts logically independent of Linq. 虽然广泛用于Linq,但Action和Func在概念上与Linq无关。 C++ already contained the basic concept in form of typed function pointers. C ++已经包含了类型化函数指针形式的基本概念。

Here is a small example for Action and Func without using Linq: 这是Action和Func的一个小例子,不使用Linq:

class Program
{
    static void Main(string[] args)
    {
        Action<int> myAction = new Action<int>(DoSomething);
        myAction(123);           // Prints out "123"
                                 // can be also called as myAction.Invoke(123);

        Func<int, double> myFunc = new Func<int, double>(CalculateSomething);
        Console.WriteLine(myFunc(5));   // Prints out "2.5"
    }

    static void DoSomething(int i)
    {
        Console.WriteLine(i);
    }

    static double CalculateSomething(int i)
    {
        return (double)i/2;
    }
}

#3楼

The difference between Func and Action is simply whether you want the delegate to return a value (use Func ) or not (use Action ). FuncAction之间的区别仅在于您是否希望委托返回值(使用Func )或不使用(使用Action )。

Func is probably most commonly used in LINQ - for example in projections: Func可能是LINQ中最常用的 - 例如在投影中:

 list.Select(x => x.SomeProperty)

or filtering: 或过滤:

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

or key selection: 或关键选择:

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

Action is more commonly used for things like List<T>.ForEach : execute the given action for each item in the list. Action更常用于List<T>.ForEach :对列表中的每个项执行给定的操作。 I use this less often than Func , although I do sometimes use the parameterless version for things like Control.BeginInvoke and Dispatcher.BeginInvoke . 我用这个少往往比Func ,虽然我有时会使用,例如,无参数的版本Control.BeginInvokeDispatcher.BeginInvoke

Predicate is just a special cased Func<T, bool> really, introduced before all of the Func and most of the Action delegates came along. Predicate只是一个特殊的套装Func<T, bool>真的,在所有Func和大多数Action代表出现之前引入。 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. 我怀疑,如果我们已经有各种伪装的FuncAction ,那么Predicate就不会被引入......尽管它确实赋予了代表使用某种意义,而FuncAction则被广泛地使用了目的。

Predicate is mostly used in List<T> for methods like FindAll and RemoveAll . Predicate主要用于List<T>用于FindAllRemoveAll等方法。


#4楼

Func - When you want a delegate for a function that may or may not take parameters and returns a value. Func - 当您想要一个函数的委托时,该函数可能会也可能不会获取参数并返回一个值。 The most common example would be Select from LINQ: 最常见的例子是从LINQ中选择:

var result = someCollection.Select( x => new { x.Name, x.Address });

Action - When you want a delegate for a function that may or may not take parameters and does not return a value. 操作 - 当您希望某个函数的委托可能带有或不带参数但不返回值时。 I use these often for anonymous event handlers: 我经常将它们用于匿名事件处理程序:

button1.Click += (sender, e) => { /* Do Some Work */ }

Predicate - When you want a specialized version of a Func that evaluates a value against a set of criteria and returns a boolean result (true for a match, false otherwise). 谓词 - 当你想要一个特殊版本的Func,它根据一组条件评估一个值并返回一个布尔结果(匹配为true,否则为false)。 Again, these are used in LINQ quite frequently for things like Where: 同样,这些在LINQ中经常用于像Where这样的东西:

 
 
 
  
  var filteredResults = someCollection.Where(x => x.someCriteriaHolder == someCriteria);
 
  

I just double checked and it turns out that LINQ doesn't use Predicates. 我只是仔细检查,结果是LINQ不使用Predicates。 Not sure why they made that decision...but theoretically it is still a situation where a Predicate would fit. 不确定他们为什么做出这个决定......但理论上它仍然是谓词适合的情况。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值