=> Lambda operator in C#, how does it work here?

I am new to C# and I’m not sure I understand this piece of code:

@Html.DisplayNameFor(model => model.isAlive)

From what I searched online, the lambda operator would be shorthand for a function such as
parameter => expression.

  • Is this what is being used here?
  • If so, is the function being called automatically since it has no name?
  • Is a model object being implicitly passed in the ‘model’ parameter?

This is the full code:

@model IEnumerable<FirstMVC.Models.PersonModel> //retrieving the passed list (people) object, it becomes IEnumerable

@{
    ViewBag.Title = "ListPeople";
}

<h2>ListPeople</h2>

<p> <!--Here, only 2 params because it's assuming using PeopleController-->
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th><!--Displays column NAMES, notice DisplayNameFor-->
            @Html.DisplayNameFor(model => model.FirstName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.LastName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Age)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.isAlive)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td> <!--Displays row VALUES-->
            @Html.DisplayFor(modelItem => item.FirstName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LastName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Age)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.isAlive)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>
    </tr>
}

</table>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Is this what is being used here?

Yes, it is defining an expression to be used

If so, is the function being called automatically since it has no name?

No. Just the expression is being passed, not the result of that expression. In this case, the expression itself is never called – it is just analyzed to determine what member is being referenced (the isAlive property in this case) so that the input can be created, named, and bound to the member on callback.

Is a model object being implicitly passed in the ‘model’ parameter?

A model is not being “passed”, but the Razor compiler will use the type defined in the @model {typename} directive to determine the type of the model parameter.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x