The rise of the lambda "goes-to" operator =>
has seen a surge in a variety of languages and now C# 6.0 allows members to be defined with this syntax instead of the traditional {}
block.
Code
C#
// Takes and returns a value
public string GetPostExcerpt(Post p)
=> string.Join(' ', p.Body.Split(' ').Take(25)) + " ...";
// No return type
public void StartCache() => cache.Start(this, cachePolicy);
C#
// Takes and returns a value
public string GetPostExcerpt(Post p) {
return string.Join(' ', p.Body.Split(' ').Take(25)) + " ...";
}
// No return type
public void StartCache()
{
cache.Start(this, cachePolicy);
}
Note
return
is automatically implied for any member with a return type and only a single statement- Support was extended to constructors, properties, finalizers and indexers with Expanded expression bodied members C# 7.0
- Unlike Lambda expressions C# 3.0 multi-line operations are not permitted
- There would be little point as it would be longer as unlike JavaScript C# does not use the
function
keyword
- There would be little point as it would be longer as unlike JavaScript C# does not use the