本文翻译自:Func vs. Action vs. Predicate [duplicate]
This question already has an answer here: 这个问题在这里已有答案:
- Delegates: Predicate Action Func 8 answers 代表:Predicate Action Func 8个答案
With real examples and their use, can someone please help me understand: 通过实例和它们的使用,有人可以帮助我理解:
- When do we need Func delegate? 我们什么时候需要Func委托?
- When do we need Action delegate? 我们什么时候需要行动委托?
- 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 ). Func和Action之间的区别仅在于您是否希望委托返回值(使用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.BeginInvoke和Dispatcher.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. 我怀疑,如果我们已经有各种伪装的Func和Action ,那么Predicate就不会被引入......尽管它确实赋予了代表使用某种意义,而Func和Action则被广泛地使用了目的。
Predicate is mostly used in List<T> for methods like FindAll and RemoveAll . Predicate主要用于List<T>用于FindAll和RemoveAll等方法。
#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. 不确定他们为什么做出这个决定......但理论上它仍然是谓词适合的情况。
本文详细解释了Func、Action和Predicate三种委托的区别及其在编程中的应用。Func用于可能需要参数并返回值的函数,常见于LINQ的投影;Action用于不需要返回值的函数,常用于事件处理;Predicate作为Func的一种特例,用于根据条件评估值并返回布尔结果,常用于List<T>的查找和移除。
1388

被折叠的 条评论
为什么被折叠?



