What is the difference between a lambda expression and a predicate in .NET?

后端 未结 2 878
醉话见心
醉话见心 2021-02-07 09:51

What is the difference between a lambda expression and a predicate in .NET?

相关标签:
2条回答
  • 2021-02-07 10:07

    A predicate is delegate (function object) that returns a boolean value. Lambda expressions can be used to define any anonymous function, which includes predicates, e.g. to express a predicate in the form of a lambda expression:

    Predicate<int> isEven2 = x => x % 2 == 0;
    

    which is functionally equivalent to:

    Func<int,bool> isEven = x => x % 2 == 0;
    
    0 讨论(0)
  • 2021-02-07 10:26

    Predicate defines a set of criteria, while lambda expression is an anonymous function. You can use lambda ex. as a predicate, but that doesn't mean they are the same thing.

    Predicate

    Lambda expression

    0 讨论(0)
提交回复
热议问题