Use of predicate and lambda expression

Asked

Viewed 87 times

7

I am studying WPF and the MVVM model by the following article : https://msdn.microsoft.com/en-us/magazine/dd419663.aspx

In the implementation of the Icommand interface, the author defines the following field:

Predicate<object> _canExecute;               

When the class is instantiated the following parameter is passed to that field:

x => canExecute

My question is, why the parameter is passed in lambda expression form?

  • 4

    TL;DR; Because it’s shorter, simpler, easier to read and, after all, more beautiful =).

1 answer

2


Lambda expressions are very useful for creating queries with LINQ in Visual C#, and are nothing more than anonymous functions that you can use to create delegates and write local functions that can be passed as arguments or returned as value in calls from other functions.

A predicate on the other hand is a delegate that returns a boolean (0 or 1, V or F, true or false, yes or no, yes or no).

Thus, you can evaluate that the use of a lambda expression, given the context, is done because it is expected of a predicate that returns a Boolean value, but this being an object passed as a function (Function Object), which is typical of a delegate, can therefore be passed as parameter after evaluated even being a function, which is typical of lambda functions or expressions.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.