C#表达式体方法 (expression-bodied method ) - 0023

时间:2020-06-15 23:02:01   收藏:0   阅读:132

如果方法的实现只有一条语句,可以使用一个简化的语法:表达式体方法。

列如方法:

public bool IsSquare(Rectangle rect)
{
	return (rect.Height == rect.Width);
}

和:

public int Sum(int x, int y)
{
      return x + y;
}

  

可以写成:

public bool IsSquare(Rectangle rect) => rect.Height == rect.Width;

和:

public int Sum(int x, int y) => x + y;

  

注意:

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!