- C# 基础教程
- C# - 首页
- C# - 概述
- C# - 环境
- C# - 程序结构
- C# - 基本语法
- C# - 数据类型
- C# - 类型转换
- C# - 变量
- C# - 常量
- C# - 运算符
- C# - 决策
- C# - 循环
- C# - 封装
- C# - 方法
- C# - 可空类型
- C# - 数组
- C# - 字符串
- C# - 结构体
- C# - 枚举
- C# - 类
- C# - 继承
- C# - 多态
- C# - 运算符重载
- C# - 接口
- C# - 命名空间
- C# - 预处理器指令
- C# - 正则表达式
- C# - 异常处理
- C# - 文件 I/O
C# - 方法
方法是一组语句,这些语句共同执行一项任务。每个 C# 程序至少有一个类,其中包含一个名为 Main 的方法。
要使用方法,您需要 -
- 定义方法
- 调用方法
在 C# 中定义方法
当您定义方法时,您基本上声明了其结构的元素。在 C# 中定义方法的语法如下所示 -
<Access Specifier> <Return Type> <Method Name>(Parameter List) {
Method Body
}
以下是方法的各个元素 -
访问修饰符 - 这决定了另一个类中变量或方法的可见性。
返回值类型 - 方法可能会返回值。返回值类型是方法返回的值的数据类型。如果方法没有返回值,则返回值类型为void。
方法名称 - 方法名称是唯一的标识符,并且区分大小写。它不能与类中声明的任何其他标识符相同。
参数列表 - 参数包含在括号内,用于传递和接收来自方法的数据。参数列表指的是方法的参数的类型、顺序和数量。参数是可选的;也就是说,方法可能不包含任何参数。
方法体 - 这包含完成所需活动所需的一组指令。
示例
以下代码片段显示了一个函数 FindMax,它接受两个整数值并返回两者中较大的一个。它具有公共访问修饰符,因此可以使用类的实例从类外部访问它。
class NumberManipulator {
public int FindMax(int num1, int num2) {
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
...
}
在 C# 中调用方法
您可以使用方法的名称来调用方法。以下示例说明了这一点 -
using System;
namespace CalculatorApplication {
class NumberManipulator {
public int FindMax(int num1, int num2) {
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
static void Main(string[] args) {
/* local variable definition */
int a = 100;
int b = 200;
int ret;
NumberManipulator n = new NumberManipulator();
//calling the FindMax method
ret = n.FindMax(a, b);
Console.WriteLine("Max value is : {0}", ret );
Console.ReadLine();
}
}
}
当编译并执行上述代码时,它会产生以下结果 -
Max value is : 200
您还可以使用类的实例从其他类调用公共方法。例如,方法 FindMax 属于 NumberManipulator 类,您可以从另一个类 Test 调用它。
using System;
namespace CalculatorApplication {
class NumberManipulator {
public int FindMax(int num1, int num2) {
/* local variable declaration */
int result;
if(num1 > num2)
result = num1;
else
result = num2;
return result;
}
}
class Test {
static void Main(string[] args) {
/* local variable definition */
int a = 100;
int b = 200;
int ret;
NumberManipulator n = new NumberManipulator();
//calling the FindMax method
ret = n.FindMax(a, b);
Console.WriteLine("Max value is : {0}", ret );
Console.ReadLine();
}
}
}
当编译并执行上述代码时,它会产生以下结果 -
Max value is : 200
递归方法调用
方法可以调用自身。这称为递归。以下是一个使用递归函数计算给定数字的阶乘的示例 -
using System;
namespace CalculatorApplication {
class NumberManipulator {
public int factorial(int num) {
/* local variable declaration */
int result;
if (num == 1) {
return 1;
} else {
result = factorial(num - 1) * num;
return result;
}
}
static void Main(string[] args) {
NumberManipulator n = new NumberManipulator();
//calling the factorial method {0}", n.factorial(6));
Console.WriteLine("Factorial of 7 is : {0}", n.factorial(7));
Console.WriteLine("Factorial of 8 is : {0}", n.factorial(8));
Console.ReadLine();
}
}
}
当编译并执行上述代码时,它会产生以下结果 -
Factorial of 6 is: 720 Factorial of 7 is: 5040 Factorial of 8 is: 40320
向方法传递参数
调用带参数的方法时,需要将参数传递给方法。参数可以通过三种方式传递给方法 -
| 序号 | 机制和描述 |
|---|---|
| 1 | 值参数
此方法将参数的实际值复制到函数的形式参数中。在这种情况下,在函数内部对参数进行的更改不会影响参数。 |
| 2 | 引用参数
此方法将参数的内存位置的引用复制到形式参数中。这意味着对参数所做的更改会影响参数。 |
| 3 | 输出参数
此方法有助于返回多个值。 |