您当前的位置: 首页 >  c#

C#中return语句的使用方法

发布时间:2010-11-22 10:59:00 ,浏览量:0

C#中return语句的使用方法 return语句终止它所在的方法的执行,并将控制权返回给调用方法,另外,它还可以返回一个可选值。如果方法为void类型,则可以省略return语句。

return语句后面可以是常量,变量,表达式,方法,也可以什么都不加。return语句可以出现在方法的任何位置。一个方法中也可以出现多个return,但只有一个会执行。当return语句后面什么都不加时,返回的类型为void。 下面代码演示retrun语句:

return 1; return a; return a+b; return Add(a+b); return;

下面编写方法计算长方形面积:用构造函数初始化对象,输出计算结果。

using System; public class Rectangle { double height, width, area; public Rectangle(double _height, double _width) {     this.height = _height;     this.width = _width; }

public double GetArea() {     area = height * width;     return area; } public void Show() {     Console.WriteLine("该长方形面积是{0}", area); } }

class Progarm { static void Main() {     Rectangle r = new Rectangle(10, 12);     r.GetArea();     r.Show(); } }

关注
打赏
1688896170
查看更多评论

暂无认证

  • 0浏览

    0关注

    104724博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.3318s