In .NET 4.0, have a built-in delegate method:
public delegate TResult Func<in T, out TResult>(T arg);
It is used in LINQ extesion methods, example:
IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
I don’t understand clearly about Func delegate, why does the following lambda expression match it:
// p is a XElement object
p=>p.Element("firstname").Value.StartsWith("Q")
Func<T,TResult>simply means: a method that accepts aTas a parameter, and returns aTResult. Your lambda matches it, in that forT=XElementandTResult=bool, your lambda takes aTand returns aTResult. In that particular case it would commonly be referred to as a predicate. The compiler can infer the generic type arguments (TandTResult) based on usage in many (not all) scenarios.Note the
inandoutrefer to the (co|contra)-variance behaviour of the method – not the normal usage ofout(i.e.outhere doesn’t mean by-ref, not assumed to be assigned on call, and needs to be assigned before exit).