一個(gè)方法是把一些相關(guān)的語句組織在一起,用來執(zhí)行一個(gè)任務(wù)的語句塊。每一個(gè) C# 程序至少有一個(gè)帶有 Main 方法的類。
要使用一個(gè)方法,您需要:
當(dāng)定義一個(gè)方法時(shí),從根本上說是在聲明它的結(jié)構(gòu)的元素。在 C# 中,定義方法的語法如下:
<Access Specifier> <Return Type> <Method Name>(Parameter List)
{
Method Body
}
下面是方法的各個(gè)元素:
下面的代碼片段顯示一個(gè)函數(shù) FindMax,它接受兩個(gè)整數(shù)值,并返回兩個(gè)中的較大值。它有 public 訪問修飾符,所以它可以使用類的實(shí)例從類的外部進(jìn)行訪問。
class NumberManipulator
{
public int FindMax(int num1, int num2)
{
/* 局部變量聲明 */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
...
}
您可以使用方法名調(diào)用方法。下面的實(shí)例演示了這點(diǎn):
using System;
namespace CalculatorApplication{
class NumberManipulator{
public int FindMax(int num1, int num2){
/* 局部變量聲明 */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
static void Main(string[] args){
/* 局部變量定義 */
int a = 100;
int b = 200;
int ret;
NumberManipulator n = new NumberManipulator();
//調(diào)用 FindMax 方法
ret = n.FindMax(a, b);
Console.WriteLine("最大值是: {0}", ret );
Console.ReadLine();
}
}
}
當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:
最大值是: 200
您也可以使用類的實(shí)例從另一個(gè)類中調(diào)用其他類的公有方法。例如,方法 FindMax 屬于 NumberManipulator 類,您可以從另一個(gè)類 Test 中調(diào)用它。
using System;
namespace CalculatorApplication{
class NumberManipulator{
public int FindMax(int num1, int num2){
/* 局部變量聲明 */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
}
class Test{
static void Main(string[] args){
/* 局部變量定義 */
int a = 100;
int b = 200;
int ret;
NumberManipulator n = new NumberManipulator();
//調(diào)用 FindMax 方法
ret = n.FindMax(a, b);
Console.WriteLine("最大值是: {0}", ret );
Console.ReadLine();
}
}
}
當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:
最大值是: 200
一個(gè)方法可以自我調(diào)用。這就是所謂的 遞歸。下面的實(shí)例使用遞歸函數(shù)計(jì)算一個(gè)數(shù)的階乘:
using System;
namespace CalculatorApplication{
class NumberManipulator{
public int factorial(int num){
/* 局部變量定義 */
int result;
if (num == 1){
return 1;
}
else{
result = factorial(num - 1) * num;
return result;
}
}
static void Main(string[] args){
NumberManipulator n = new NumberManipulator();
//調(diào)用 factorial 方法
Console.WriteLine("6 的階乘是: {0}", n.factorial(6));
Console.WriteLine("7 的階乘是: {0}", n.factorial(7));
Console.WriteLine("8 的階乘是: {0}", n.factorial(8));
Console.ReadLine();
}
}
}
當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:
6 的階乘是: 720
7 的階乘是: 5040
8 的階乘是: 40320
當(dāng)調(diào)用帶有參數(shù)的方法時(shí),您需要向方法傳遞參數(shù)。在 C# 中,有三種向方法傳遞參數(shù)的方式:
方式 | 描述 |
---|---|
值參數(shù) | 這種方式復(fù)制參數(shù)的實(shí)際值給函數(shù)的形式參數(shù),實(shí)參和形參使用的是兩個(gè)不同內(nèi)存中的值。在這種情況下,當(dāng)形參的值發(fā)生改變時(shí),不會(huì)影響實(shí)參的值,從而保證了實(shí)參數(shù)據(jù)的安全。 |
引用參數(shù) | 這種方式復(fù)制參數(shù)的內(nèi)存位置的引用給形式參數(shù)。這意味著,當(dāng)形參的值發(fā)生改變時(shí),同時(shí)也改變實(shí)參的值。 |
輸出參數(shù) | 這種方式可以返回多個(gè)值。 |
這是參數(shù)傳遞的默認(rèn)方式。在這種方式下,當(dāng)調(diào)用一個(gè)方法時(shí),會(huì)為每個(gè)值參數(shù)創(chuàng)建一個(gè)新的存儲(chǔ)位置。
實(shí)際參數(shù)的值會(huì)復(fù)制給形參,實(shí)參和形參使用的是兩個(gè)不同內(nèi)存中的值。所以,當(dāng)形參的值發(fā)生改變時(shí),不會(huì)影響實(shí)參的值,從而保證了實(shí)參數(shù)據(jù)的安全。下面的實(shí)例演示了這個(gè)概念:
using System;
namespace CalculatorApplication{
class NumberManipulator{
public void swap(int x, int y){
int temp;
temp = x; /* 保存 x 的值 */
x = y; /* 把 y 賦值給 x */
y = temp; /* 把 temp 賦值給 y */
}
static void Main(string[] args){
NumberManipulator n = new NumberManipulator();
/* 局部變量定義 */
int a = 100;
int b = 200;
Console.WriteLine("在交換之前,a 的值: {0}", a);
Console.WriteLine("在交換之前,b 的值: {0}", b);
/* 調(diào)用函數(shù)來交換值 */
n.swap(a, b);
Console.WriteLine("在交換之后,a 的值: {0}", a);
Console.WriteLine("在交換之后,b 的值: {0}", b);
Console.ReadLine();
}
}
}
當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:
在交換之前,a 的值:100
在交換之前,b 的值:200
在交換之后,a 的值:100
在交換之后,b 的值:200
結(jié)果表明,即使在函數(shù)內(nèi)改變了值,值也沒有發(fā)生任何的變化。
引用參數(shù)是一個(gè)對(duì)變量的內(nèi)存位置的引用。當(dāng)按引用傳遞參數(shù)時(shí),與值參數(shù)不同的是,它不會(huì)為這些參數(shù)創(chuàng)建一個(gè)新的存儲(chǔ)位置。引用參數(shù)表示與提供給方法的實(shí)際參數(shù)具有相同的內(nèi)存位置。
在 C# 中,使用 ref 關(guān)鍵字聲明引用參數(shù)。下面的實(shí)例演示了這點(diǎn):
using System;
namespace CalculatorApplication{
class NumberManipulator{
public void swap(ref int x, ref int y){
int temp;
temp = x; /* 保存 x 的值 */
x = y; /* 把 y 賦值給 x */
y = temp; /* 把 temp 賦值給 y */
}
static void Main(string[] args){
NumberManipulator n = new NumberManipulator();
/* 局部變量定義 */
int a = 100;
int b = 200;
Console.WriteLine("在交換之前,a 的值: {0}", a);
Console.WriteLine("在交換之前,b 的值: {0}", b);
/* 調(diào)用函數(shù)來交換值 */
n.swap(ref a, ref b);
Console.WriteLine("在交換之后,a 的值: {0}", a);
Console.WriteLine("在交換之后,b 的值: {0}", b);
Console.ReadLine();
}
}
}
當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:
在交換之前,a 的值:100
在交換之前,b 的值:200
在交換之后,a 的值:200
在交換之后,b 的值:100
結(jié)果表明,swap 函數(shù)內(nèi)的值改變了,且這個(gè)改變可以在 Main 函數(shù)中反映出來。
return 語句可用于只從函數(shù)中返回一個(gè)值。但是,可以使用 輸出參數(shù) 來從函數(shù)中返回兩個(gè)值。輸出參數(shù)會(huì)把方法輸出的數(shù)據(jù)賦給自己,其他方面與引用參數(shù)相似。
下面的實(shí)例演示了這點(diǎn):
using System;
namespace CalculatorApplication{
class NumberManipulator{
public void getValue(out int x ){
int temp = 5;
x = temp;
}
static void Main(string[] args){
NumberManipulator n = new NumberManipulator();
/* 局部變量定義 */
int a = 100;
Console.WriteLine("在方法調(diào)用之前,a 的值: {0}", a);
/* 調(diào)用函數(shù)來獲取值 */
n.getValue(out a);
Console.WriteLine("在方法調(diào)用之后,a 的值: {0}", a);
Console.ReadLine();
}
}
}
當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:
在方法調(diào)用之前,a 的值: 100
在方法調(diào)用之后,a 的值: 5
提供給輸出參數(shù)的變量不需要賦值。當(dāng)需要從一個(gè)參數(shù)沒有指定初始值的方法中返回值時(shí),輸出參數(shù)特別有用。請(qǐng)看下面的實(shí)例,來理解這一點(diǎn):
using System;
namespace CalculatorApplication{
class NumberManipulator{
public void getValues(out int x, out int y ){
Console.WriteLine("請(qǐng)輸入第一個(gè)值: ");
x = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("請(qǐng)輸入第二個(gè)值: ");
y = Convert.ToInt32(Console.ReadLine());
}
static void Main(string[] args){
NumberManipulator n = new NumberManipulator();
/* 局部變量定義 */
int a , b;
/* 調(diào)用函數(shù)來獲取值 */
n.getValues(out a, out b);
Console.WriteLine("在方法調(diào)用之后,a 的值: {0}", a);
Console.WriteLine("在方法調(diào)用之后,b 的值: {0}", b);
Console.ReadLine();
}
}
}
當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果(取決于用戶輸入):
請(qǐng)輸入第一個(gè)值:
7
請(qǐng)輸入第二個(gè)值:
8
在方法調(diào)用之后,a 的值: 7
在方法調(diào)用之后,b 的值: 8
更多建議: