It's a class.
Expression<Func<TModel, TProperty>>
encapsulates Func<TModel, TProperty>
, and contains extra metadata which can be used by third parties to rebuild the Func
in their own native language.
Func<TSrc, TDest>
represents a function that takes TSrc
as its input, and generates an instance of TDest
as its output. For example, I can say:
var addOne = new Func<int, int>(i => i + 1);
and addOne
would be a mapping that takes an integer and gives back that integer plus one. After you've defined it, you can then call addOne(1)
, and you'd expect that to give you 2.
The thing inside the brackets -- the i => i + 1 -- is a piece of syntax called a lambda. Confusingly, a lambda can be used to represent both a Func<TSrc, TDest>
and an Expression<Func<TSrc, TDest>>
. That is, depending on the context,
i => i + 1
represents either a Func<int, int>
object or an Expression<Func<int, int>>
object. This is an example (my favourite example) of homoiconicity in a language. That is, a syntax in which you can have objects of different classes represented by the same symbol, or icon.
Going back to MVC, the reason it wants an Expression<Func<TModel, TProperty>>
rather than just a plan old Func<TModel, TProperty>
is because it wants to know more about the property than what that property does with the model instance: it wants to know extra data such as the property name, its attributes, and what kind of error handling it uses. So you can give it something like
@Html.TextBoxFor(m => m.FirstName, ...)
and MVC will look up the property FirstName
in your model and build up all sorts of HTML based on the attributes you've defined on that property.
That's more or less how it works.