Expression-bodied function members allow the use of lambda expressions as member bodies. For simple members, it can result in cleaner and more readable code.
Expression-bodied functions can be used for properties, indexers, methods, and operators.
public decimal TotalPrice => BasePrice + Taxes;
Is equivalent to:
public decimal TotalPrice
{
get
{
return BasePrice + Taxes;
}
}
When an expression-bodied function is used with a property, the property is implemented as a getter-only property.
public object this[string key] => dictionary[key];
Is equivalent to:
public object this[string key]
{
get
{
return dictionary[key];
}
}
static int Multiply(int a, int b) => a * b;
Is equivalent to:
static int Multiply(int a, int b)
{
return a * b;
}
Which can also be used with void
methods:
public void Dispose() => resource?.Dispose();
An override of ToString
could be added to the Pair<T>
class:
public override string ToString() => $"{First}, {Second}";