
- 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 | 輸出引數
此方法有助於返回多個值。 |