Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 377
Nathan Noble Avatar asked Jul 02 '12 00:07

Nathan Noble


People also ask

What is predicate in lambda expression?

Predicate<T> is a generic functional interface that represents a single argument function that returns a boolean value (true or false). This interface available in java. util. function package and contains a test(T t) method that evaluates the predicate of a given argument.

What is a predicate in C#?

Predicate in general meaning is a statement about something that is either true or false. In programming, predicates represent single argument functions that return a boolean value.

What is lambda expression in .NET framework?

You use a lambda expression to create an anonymous function. Use the lambda declaration operator => to separate the lambda's parameter list from its body. A lambda expression can be of any of the following two forms: Expression lambda that has an expression as its body: C# Copy.

What is the difference between statement lambda and expression lambda?

The difference between a statement and an expression lambda is that the statement lambda has a statement block on the right side of the lambda operator, whereas the expression lambda has only an expression (no return statement or curly braces, for example).


1 Answers

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;
like image 162
BrokenGlass Avatar answered Sep 30 '22 18:09

BrokenGlass