It important to understand that lambda is in its essence anonymous function declaration.
1) I don't understand why I can use any letter before => inside that expression and it still works
m
is just parameter name. You can write @Html.DisplayFor(m => m.TimeStamp)
or @Html.DisplayFor(i => i.TimeStamp)
and it would be (in this case! - see this) functionally equal to your code (but more efficient)
Reason why your code works is because m
parameter is not used in any way and code inside lambda (which is effectively function body) is using variable declared outside the function (code after =>
). This is called closure\captured variable - it's less efficient, have many pitfalls but can be extremely useful...
2) If that does not matter why do I have to write anything before =>?
Because @Html.DisplayFor
method is declared to take Func<TModel, TValue>
parameter which you can read as 'function which takes one argument (model) and returns single value'
3) Using normal (anonymous) method syntax:
@foreach (var comment in Model.Comments) {
<p> @Html.DisplayFor(delegate(CommentsType c) { return c.TimeStamp; })</p>
}
....where CommentsType
is type of Model.Comments
property