While researching C# operator overloading, I stumbled across this block of code on the MSDN web site:
public static Complex operator +(Complex c1, Complex c2) =>
new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
// Override ToString() to display a complex number
// in the traditional format:
public override string ToString() => $"{this.real} + {this.imaginary}";
This is a really useful way of defining simple methods in certain circumstances, but I don't recall ever seeing it described anywhere. I tried searching the C# 5.0 language specification for a description of this method declaration syntax, but could find nothing. I also found nothing in my web searches.
Two questions:
- In which version of C# did this method declaration syntax become available?
- Where in the language specification is this syntax described?
It was added in C# 6, you can read about it on the official Github of the new compiler here.