如何在C#中使用return语句?
return语句用于返回值。当程序调用函数时,程序控制将转移到被调用函数。被调用函数执行已定义的任务,并且在执行其return语句或达到其函数结尾的右括号时,它将程序控制权返回给主程序。
以下是一个了解C#中return语句用法的示例。在这里,我们找到一个数字的阶乘,并使用return语句返回结果。
while (n != 1) {
res = res * n;
n = n - 1;
}
return res;这是完整的示例。
示例
using System;
namespace Demo {
class Factorial {
public int display(int n) {
int res = 1;
while (n != 1) {
res = res * n;
n = n - 1;
}
return res;
}
static void Main(string[] args) {
int value = 5;
int ret;
Factorial fact = new Factorial();
ret = fact.display(value);
Console.WriteLine("Value is : {0}", ret );
Console.ReadLine();
}
}
}输出结果
Value is : 120
热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿