开发者

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开发者_JAVA百科 - 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜