Full method/property syntax is verbose for simple operations. Expression-bodied members reduce boilerplate.
Properties:
// Old way
public string FullName
{
get { return FirstName + " " + LastName; }
}
// Expression-bodied (C# 6+)
public string FullName => FirstName + " " + LastName;
Methods:
// Old way
public int Add(int a, int b)
{
return a + b;
}
// Expression-bodied
public int Add(int a, int b) => a + b;
Constructors (C# 7+):
// Old way
public Person(string name)
{
Name = name;
}
// Expression-bodied
public Person(string name) => Name = name;
Get/Set (C# 7+):
private string _name;
public string Name
{
get => _name;
set => _name = value?.Trim() ?? string.Empty;
}
Clean, readable, less boilerplate!
