C# method declarations using the lambda operator [

2020-03-31 05:50发布

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:

  1. In which version of C# did this method declaration syntax become available?
  2. Where in the language specification is this syntax described?

标签: c# lambda
1条回答
三岁会撩人
2楼-- · 2020-03-31 06:15

It was added in C# 6, you can read about it on the official Github of the new compiler here.

查看更多
登录 后发表回答