简体   繁体   中英

What does “(item) =>” do?

I'm trying to learn C# and MVC3. I wanted to have a WebGrid column as an Html.Action link, however, it wouldn't work until I did this:

grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { id = item.Id }))

So I know that this fixes it but why? The (item) looks like a cast but what is the => for? From reading other questions I see that it's evidently bad to do this for some reason - why?

This is known as a lambda expression / anonymous function in C#. The () portion is the argument list and the => indicates the right hand side is the body / expression of the lambda.

Here's a slightly expanded form which may be a bit clearer

Func<ItemType, string> linkFunction = (item) =>
{
  return Html.ActionLink("Edit", "Edit", new { id = item.Id });
};

That would be a lambda expression . And no, using lambda's is not bad, it's a (very) good thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM