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
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With