Alex Rivera | Logout

Why would you use Expression<Func<T>> rather than Func<T>?

Asked 2009-04-27T13:50:14.503
1160

I understand lambdas and the Func and Action delegates. But expressions stump me.

In what circumstances would you use an Expression<Func<T>> rather than a plain old Func<T>?

Edit
Report

1 Answer

89

I'd like to add some notes about the differences between Func<T> and Expression<Func<T>>:

  • Func<T> is just a normal old-school MulticastDelegate;
  • Expression<Func<T>> is a representation of lambda expression in form of expression tree;
  • expression tree can be constructed through lambda expression syntax or through the API syntax;
  • expression tree can be compiled to a delegate Func<T>;
  • the inverse conversion is theoretically possible, but it's a kind of decompiling, there is no builtin functionality for that as it's not a straightforward process;
  • expression tree can be observed/translated/modified through the ExpressionVisitor;
  • the extension methods for IEnumerable operate with Func<T>;
  • the extension methods for IQueryable operate with Expression<Func<T>>.

There's an article which describes the details with code samples:
LINQ: Func<T> vs. Expression<Func<T>>.

Hope it will be helpful.

answered 2013-06-11T00:02:19.673

Your Answer