Reputation: 32828
I am looking at code containing:
public virtual ICollection<T> GetPk(string pk)
{
Expression<Func<T, bool>> predicate = c => c.PartitionKey == pk;
return this.GetAll(predicate);
}
Can someone explain the syntax of <Func<T, bool>>
?
Upvotes: 3
Views: 7526
Reputation: 139
It's additional syntax so you know what goes in and out of the function.
Func<T, bool>
means:
function has 1 input T
and 1 output that's bool
.
This is other variations of the function
Upvotes: 0
Reputation: 5242
For some background; prior to Func<T, TResult>
(and the rest of this family) being part of the framework, you either had to explicitly define delegates or use anonymous methods.
Func
and Action
were added as part of the addition of lambda expressions to the language. They are the framework-defined delegates which lambda expressions are typed as, but which you as a developer can also use in place of your own custom delegate definitions.
You can get a nice history here;
http://blogs.msdn.com/b/ericwhite/archive/2006/10/03/lambda-expressions.aspx
Upvotes: 2
Reputation: 19217
Simply Func<T, bool>
is the anonymous method signature. The first type T
is the input parameter type and the second type is the return type. This is more like a method when you consider your representation:
bool AnonMethod(T arg0)
{
return arg0.PartitionKey == pk;
}
Upvotes: 6
Reputation: 10410
It is confusing at first but Func<T, bool>
describes a function that returns a bool and accepts a parameter as type T.
In this case, T is an object that has a PartitionKey
property and this GetPk
method is using the Func<T, bool>
to match all the T items in the instance object which have a PartitionKey
that matches the string pk
.
Upvotes: 1
Reputation: 292615
A Func<T, bool>
represents a function that takes an object of type T
and returns a bool
. It's commonly referred to as a "predicate", and is used to verify a condition on an object.
An Expression<Func<T, bool>>
represents the abstract syntax tree of the function, i.e. its syntactic structure. It can be used to analyse the code of the function for various purposes, such as transforming it to SQL to be executed against a database.
Upvotes: 4
Reputation: 2447
I always find MSDN to be worth checking on things like this first,
http://msdn.microsoft.com/en-us/library/bb549151.aspx
Beaten by Maheep, didn't see the post message pop-in.
Basically, you're declaring a method that matches a signature, that can then be passed in to the call to get the data.
Upvotes: 1
Reputation: 5605
One of the best explanation can be found at MSDN
You can use this delegate to represent a method that can be passed as a parameter without explicitly declaring a custom delegate. The encapsulated method must correspond to the method signature that is defined by this delegate. This means that the encapsulated method must have one parameter that is passed to it by value, and that it must return a value.
As for argument in your example T is the type of input parameter and bool is the return type of exacted method.
Upvotes: 5