c# 方法
一個方法是把一些相關的語句組織在一起,用來執行一個任務的語句塊。每一個 c# 程序至少有一個帶有 main 方法的類。
要使用一個方法,您需要:
- 定義方法
- 調用方法
1. c# 中定義方法
當定義一個方法時,從根本上說是在聲明它的結構的元素。在 c# 中,定義方法的語法如下:
<access specifier> <return type> <method name>(parameter list) { method body }
下面是方法的各個元素:
- access specifier:訪問修飾符,這個決定了變量或方法對于另一個類的可見性。
- return type:返回類型,一個方法可以返回一個值。返回類型是方法返回的值的數據類型。如果方法不返回任何值,則返回類型為 void。
- method name:方法名稱,是一個唯一的標識符,且是大小寫敏感的。它不能與類中聲明的其他標識符相同。
- parameter list:參數列表,使用圓括號括起來,該參數是用來傳遞和接收方法的數據。參數列表是指方法的參數類型、順序和數量。參數是可選的,也就是說,一個方法可能不包含參數。
- method body:方法主體,包含了完成任務所需的指令集。
2. c# 方法范例
下面的代碼片段顯示一個函數 findmax,它接受兩個整數值,并返回兩個中的較大值。它有 public 訪問修飾符,所以它可以使用類的范例從類的外部進行訪問。
class numbermanipulator { ? ?public int findmax(int num1, int num2) ? ?{ ? ? ? /* 局部變量聲明 */ ? ? ? int result; ? ? ? if (num1 > num2) ? ? ? ? ?result = num1; ? ? ? else ? ? ? ? ?result = num2; ? ? ? return result; ? ?} ? ?... }
3. c# 中調用方法
您可以使用方法名調用方法。下面的范例演示了這點:
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(); ? ? ? ? ?//調用 findmax 方法 ? ? ? ? ?ret = n.findmax(a, b); ? ? ? ? ?console.writeline("最大值是: {0}", ret ); ? ? ? ? ?console.readline(); ? ? ? } ? ?} }
當上面的代碼被編譯和執行時,它會產生下列結果:
最大值是: 200
您也可以使用類的范例從另一個類中調用其他類的公有方法。例如,方法 findmax 屬于 numbermanipulator 類,您可以從另一個類 test 中調用它。
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(); ? ? ? ? ? ? //調用 findmax 方法 ? ? ? ? ? ? ret = n.findmax(a, b); ? ? ? ? ? ? console.writeline("最大值是: {0}", ret ); ? ? ? ? ? ? console.readline(); ? ? ? ? } ? ? } }
當上面的代碼被編譯和執行時,它會產生下列結果:
最大值是: 200
4. 遞歸方法調用
一個方法可以自我調用。這就是所謂的 遞歸。下面的范例使用遞歸函數計算一個數的階乘:
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(); ? ? ? ? ? ? //調用 factorial 方法 ? ? ? ? ? ? console.writeline("6 的階乘是: {0}", n.factorial(6)); ? ? ? ? ? ? console.writeline("7 的階乘是: {0}", n.factorial(7)); ? ? ? ? ? ? console.writeline("8 的階乘是: {0}", n.factorial(8)); ? ? ? ? ? ? console.readline(); ? ? ? ? } ? ? } }
當上面的代碼被編譯和執行時,它會產生下列結果:
6 的階乘是: 720 7 的階乘是: 5040 8 的階乘是: 40320
5. 參數傳遞
當調用帶有參數的方法時,您需要向方法傳遞參數。在 c# 中,有三種向方法傳遞參數的方式:
方式 | 描述 |
---|---|
值參數 | 這種方式復制參數的實際值給函數的形式參數,實參和形參使用的是兩個不同內存中的值。在這種情況下,當形參的值發生改變時,不會影響實參的值,從而保證了實參數據的安全。 |
引用參數 | 這種方式復制參數的內存位置的引用給形式參數。這意味著,當形參的值發生改變時,同時也改變實參的值。 |
輸出參數 | 這種方式可以返回多個值。 |
6. 按值傳遞參數
這是參數傳遞的默認方式。在這種方式下,當調用一個方法時,會為每個值參數創建一個新的存儲位置。
實際參數的值會復制給形參,實參和形參使用的是兩個不同內存中的值。所以,當形參的值發生改變時,不會影響實參的值,從而保證了實參數據的安全。下面的范例演示了這個概念:
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); ? ? ? ? ? ? ? ? ? ?/* 調用函數來交換值 */ ? ? ? ? ?n.swap(a, b); ? ? ? ? ? ? ? ? ? ?console.writeline("在交換之后,a 的值: {0}", a); ? ? ? ? ?console.writeline("在交換之后,b 的值: {0}", b); ? ? ? ? ? ? ? ? ? ?console.readline(); ? ? ? } ? ?} }
當上面的代碼被編譯和執行時,它會產生下列結果:
在交換之前,a 的值:100 在交換之前,b 的值:200 在交換之后,a 的值:100 在交換之后,b 的值:200
結果表明,即使在函數內改變了值,值也沒有發生任何的變化。
7. 按引用傳遞參數
引用參數是一個對變量的內存位置的引用。當按引用傳遞參數時,與值參數不同的是,它不會為這些參數創建一個新的存儲位置。引用參數表示與提供給方法的實際參數具有相同的內存位置。
在 c# 中,使用 ref 關鍵字聲明引用參數。下面的范例演示了這點:
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); ? ? ? ? ?/* 調用函數來交換值 */ ? ? ? ? ?n.swap(ref a, ref b); ? ? ? ? ?console.writeline("在交換之后,a 的值: {0}", a); ? ? ? ? ?console.writeline("在交換之后,b 的值: {0}", b); ? ? ? ? ? ?console.readline(); ? ? ? } ? ?} }
當上面的代碼被編譯和執行時,它會產生下列結果:
在交換之前,a 的值:100 在交換之前,b 的值:200 在交換之后,a 的值:200 在交換之后,b 的值:100
結果表明,swap 函數內的值改變了,且這個改變可以在 main 函數中反映出來。
8. 按輸出傳遞參數
return 語句可用于只從函數中返回一個值。但是,可以使用 輸出參數 來從函數中返回兩個值。輸出參數會把方法輸出的數據賦給自己,其他方面與引用參數相似。
下面的范例演示了這點:
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("在方法調用之前,a 的值: {0}", a); ? ? ? ? ? ? ? ? ? ?/* 調用函數來獲取值 */ ? ? ? ? ?n.getvalue(out a); ? ? ? ? ?console.writeline("在方法調用之后,a 的值: {0}", a); ? ? ? ? ?console.readline(); ? ? ? } ? ?} }
當上面的代碼被編譯和執行時,它會產生下列結果:
在方法調用之前,a 的值: 100 在方法調用之后,a 的值: 5
提供給輸出參數的變量不需要賦值。當需要從一個參數沒有指定初始值的方法中返回值時,輸出參數特別有用。請看下面的范例,來理解這一點:
using system; namespace calculatorapplication { ? ?class numbermanipulator ? ?{ ? ? ? public void getvalues(out int x, out int y ) ? ? ? { ? ? ? ? ? console.writeline("請輸入第一個值: "); ? ? ? ? ? x = convert.toint32(console.readline()); ? ? ? ? ? console.writeline("請輸入第二個值: "); ? ? ? ? ? y = convert.toint32(console.readline()); ? ? ? } ? ? ? ? ? static void main(string[] args) ? ? ? { ? ? ? ? ?numbermanipulator n = new numbermanipulator(); ? ? ? ? ?/* 局部變量定義 */ ? ? ? ? ?int a , b; ? ? ? ? ? ? ? ? ? ?/* 調用函數來獲取值 */ ? ? ? ? ?n.getvalues(out a, out b); ? ? ? ? ?console.writeline("在方法調用之后,a 的值: {0}", a); ? ? ? ? ?console.writeline("在方法調用之后,b 的值: {0}", b); ? ? ? ? ?console.readline(); ? ? ? } ? ?} }
當上面的代碼被編譯和執行時,它會產生下列結果(取決于用戶輸入):
請輸入第一個值: 7 請輸入第二個值: 8 在方法調用之后,a 的值: 7 在方法調用之后,b 的值: 8