How to assign an expression to a method in C#6
February 15, 2016 1 Comment
In this post we saw how to assign expressions to class properties in C# 6 using the lambda operator “=>”:
public string FullName => string.Format("{0} {1}", FirstName, LastName);
This property getter was part of a Person class:
public class Person { public int Age { get; } public string FirstName { get;} public string LastName { get; } public string FullName => string.Format("{0} {1}", FirstName, LastName); public Person(string firstName, string lastName, int age) { FirstName = firstName; LastName = lastName; Age = age; } }
The same type of syntax exists for one-liner functions. It’s really just syntactic sugar which saves you a return statement and the brackets.