Brendan
Brendan

Reputation: 1496

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

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

Upvotes: 14

Views: 12500

Answers (4)

Eric Lippert
Eric Lippert

Reputation: 660417

This method is probably a member of a collection type, yes?

A "predicate" is any device that says "yes" or "no" to the question "is this thing a member of that set?" So a predicate for the set "integers even positive integers" would be x=> x > 0 && x % 2 == 0.

This method probably has the semantics of "delete from the collection all members of the collection that are in the set identified by the predicate".

The predicate is passed to the method in the form of an expression tree, which is a way of passing the structure of the predicate in a manner that can be analyzed at runtime and transformed. It is typically used in scenarios where the "collection" is actually a database somewhere, and the deletion request needs to be translated into a query in the database's query language and sent over the network.

Upvotes: 20

James Shuttler
James Shuttler

Reputation: 1374

While the method signature looks invalid to me, essentially you are passing in an expression tree (it might not be a LambdaExpression type as Expression is the abstract base class for all expression types).

The type constraints state that T must be a reference type (inherit from a class, cannot be a value type (read: struct)) and must also have a default constructor defined.

EDIT: see Jon's answer below, he corrected the signature and answered the question correctly from there, providing more information than I.

Upvotes: 0

Shahar Gvirtz
Shahar Gvirtz

Reputation: 2438

this methods gets as a parameter expression tree of function that gets object with public parameter-less constructor and returns boolean.

you can read more about expression trees and their usage here: http://msdn.microsoft.com/en-us/library/bb397951.aspx

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1502816

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.

Upvotes: 9

Related Questions