使用Lambda运算符的C#方法声明 [英] C# method declarations using the lambda operator

查看:75
本文介绍了使用Lambda运算符的C#方法声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在研究 C#运算符重载时,我偶然发现了MSDN网站上的代码:

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}";

在某些情况下,这是定义简单方法的一种非常有用的方法,但是我不记得曾经在任何地方看到它的描述.我尝试在C#5.0语言规范中搜索此方法声明语法的描述,但找不到任何内容.我在网络搜索中也没有找到任何内容.

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.

两个问题:

  1. 此方法声明语法在哪个C#版本中可用?
  2. 在语言规范的何处描述了这种语法?

推荐答案

它是C#6中添加的,您可以在新编译器的官方Github上阅读它

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

这篇关于使用Lambda运算符的C#方法声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆