函数、动作、谓词

发布于 2024-10-05 19:21:55 字数 533 浏览 8 评论 0原文

通过真实的例子及其使用,有人可以帮助我理解:

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

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

  1. When do we need a Func<T, ..> delegate?
  2. When do we need an Action<T> delegate?
  3. When do we need a Predicate<T> delegate?

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

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

发布评论

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

评论(3

不必你懂 2024-10-12 19:21:55

FuncAction 之间的区别只是在于您是否希望委托返回值(使用 Func)或不返回值(使用 Action )。

Func 可能在 LINQ 中最常用 - 例如在投影中:

 list.Select(x => x.SomeProperty)

或过滤:

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

或键选择:

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

Action 更常用于诸如 List之类的事情。 .ForEach:为列表中的每个项目执行给定的操作。我使用它的频率比 Func 少,尽管我有时会使用无参数版本来处理 Control.BeginInvokeDispatcher.BeginInvoke 之类的事情

Predicate 实际上只是一个特殊的 Func,在所有 Func 和大部分 Action< 之前引入/code> 代表们也来了。我怀疑,如果我们已经有了各种形式的 FuncAction,就不会引入 Predicate...尽管它确实为委托的使用赋予了一定的含义,而FuncAction则用于广泛不同的目的。

Predicate 主要用于 List 中,用于 FindAllRemoveAll 等方法。

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

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

 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. I use this less often than Func, although I do sometimes use the parameterless version for things like Control.BeginInvoke and 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. 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 is mostly used in List<T> for methods like FindAll and RemoveAll.

一人独醉 2024-10-12 19:21:55

Action 是一个方法的委托(指针),它接受零个、一个或多个输入参数,但不返回任何内容。

Func 是一个方法的委托(指针),它接受零个、一个或多个输入参数,并返回一个值(或引用)。

Predicate 是一种特殊的 Func,通常用于比较(采用通用参数并返回 bool)。

尽管 Action 和 Func 广泛与 Linq 一起使用,但它们在逻辑上是独立于 Linq 的概念。 C++ 已经包含了类型化函数指针形式的基本概念。

下面是一个不使用 Linq 的 Action 和 Func 的小示例:

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;
    }
}

Action is a delegate (pointer) to a method, that takes zero, one or more input parameters, but does not return anything.

Func is a delegate (pointer) to a method, that takes zero, one or more input parameters, and returns a value (or reference).

Predicate is a special kind of Func often used for comparisons (takes a generic parameter and returns bool).

Though widely used with Linq, Action and Func are concepts logically independent of Linq. C++ already contained the basic concept in form of typed function pointers.

Here is a small example for Action and Func without using 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;
    }
}
空‖城人不在 2024-10-12 19:21:55

Func - 当您想要一个函数的委托时,该函数可能会也可能不会接受参数并返回值。最常见的示例是从 LINQ 中选择:

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

操作 - 当您想要一个可能接受或不接受参数且不返回值的函数的委托时。我经常将这些用于匿名事件处理程序:

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

Predicate - 当您想要一个专门版本的 Func 来根据一组条件评估值并返回布尔结果(匹配时为 true,否则为 false) 。 同样,这些在 LINQ 中非常频繁地用于诸如Where之类的事情:

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

我刚刚仔细检查过,结果发现 LINQ 不使用谓词。不知道他们为什么做出这个决定……但从理论上讲,这仍然是谓词适合的情况。

Func - When you want a delegate for a function that may or may not take parameters and returns a value. The most common example would be Select from 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). Again, these are used in LINQ quite frequently for things like Where:

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

I just double checked and it turns out that LINQ doesn't use Predicates. Not sure why they made that decision...but theoretically it is still a situation where a Predicate would fit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文