Alex Rivera | Logout

What does the Expression<Func<T,bool>> declaration mean?

Asked 2012-01-20T14:19:30.950
14

Could somebody explain the following declaration in a way that conveys the meaning of the expression and how it would be called?

void Delete<T>(Expression<Func<T, bool>> expression) where T : class, new();

I read it as: Delete an object of type T, by passing in a lambda expression whose parameter is an object of type T that returns a bool.

Also, can you replace Func<T, bool> expression with Predicate<T> expression

Edit
Report

1 Answer

9

The first is a method which accepts an expression tree (not necessarily created from a lambda expression tree). The expression tree represents an expression which accepts a T and returns a bool. T is constrained to be a reference type with a parameterless constructor.

As for the semantic meaning - that's up to the documentation/implementation.

It's important to distinguish between a lambda expression, which is one way of creating an expression tree, and an expression tree itself.

As for whether it could use Predicate<T> instead - maybe. It depends on what the implementation does with it. They represent the same delegate signature, certainly - but you can't convert between the two types of expression tree trivially.

answered 2012-01-20T14:23:59.513

Your Answer