鍍金池/ 教程/ C#/ 方法
循環(huán)
正則表達(dá)式
概述
委托
多態(tài)性
字符串
繼承
結(jié)構(gòu)體
集合
變量
不安全代碼
判斷
反射
異常處理
可空類型
方法
數(shù)據(jù)類型
命名空間
文件 I/O
類型轉(zhuǎn)換
屬性
程序結(jié)構(gòu)
事件
接口
預(yù)處理指令
運(yùn)算符
多線程
匿名方法
索引器
泛型
封裝
常量和文字
基本語法
特性
數(shù)組
環(huán)境配置
運(yùn)算符重載
枚舉

方法

方法是一組在一起執(zhí)行任務(wù)的語句。每個(gè) C# 程序都至少有一個(gè)含有方法的類,名為 Main。
若要使用方法,您需要:

  • 定義一個(gè)方法
  • 調(diào)用方法

在 C# 中定義方法

當(dāng)你定義一個(gè)方法時(shí),你基本上要聲明其結(jié)構(gòu)的組成元素。在 C# 中定義方法的語法如下所示:

<Access Specifier> <Return Type> <Method Name>(Parameter List)
{
   Method Body
}

以下是方法中的各種元素:

  • 訪問說明符:它用于從一個(gè)類中確定一個(gè)變量或方法的可見性。
  • 返回類型:方法可能會(huì)返回一個(gè)值。返回類型是方法返回值的數(shù)據(jù)類型。如果該方法不返回任何值,那么返回類型是 void。
  • 方法名稱:方法名稱是唯一的標(biāo)識符,并區(qū)分大小寫。它不能與在類中聲明的任何其他標(biāo)識符相同。
  • 參數(shù)列表:括號括起來,使用參數(shù)從方法中傳遞和接收數(shù)據(jù)。參數(shù)列表是指類型、順序和方法的參數(shù)數(shù)目。參數(shù)是可選的;方法可能包含任何參數(shù)。
  • 方法主體:它包含的一組指令完成所需要的活動(dòng)所需。

示例
下面的代碼段顯示了一個(gè)函數(shù) FindMax,從兩個(gè)整數(shù)值中,返回其中較大的一個(gè)。它具有公共訪問說明符,所以它可以通過使用類的外部例子來訪問。

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# 中調(diào)用方法

你可以使用方法的名稱來調(diào)用方法。下面的示例說明了這一點(diǎn):

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();
      }
   }
}

編譯執(zhí)行上述代碼,得到如下結(jié)果:

Max value is : 200

你也可以通過使用類的實(shí)例來從其他類中調(diào)用公開方法。
例如,F(xiàn)indMax 它屬于 NumberManipulator 類中的方法,你可以從另一個(gè)類測試中調(diào)用它。

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();
      }
   }
}

編譯執(zhí)行上述代碼,得到如下結(jié)果:

Max value is : 200

遞歸方法調(diào)用

有一種方法可以調(diào)用本身。這就是所謂的遞歸。下面是使用遞歸函數(shù)計(jì)算一個(gè)給定數(shù)字階乘的示例:

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
         Console.WriteLine("Factorial of 6 is : {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();
      }
   }
}

編譯執(zhí)行上述代碼,得到如下結(jié)果:

Factorial of 6 is: 720
Factorial of 7 is: 5040
Factorial of 8 is: 40320

將參數(shù)傳遞給方法

當(dāng)調(diào)用帶參數(shù)的方法時(shí),您需要將參數(shù)傳遞給該方法。有三種方法,可以將參數(shù)傳遞給方法:

方法 描述
值參數(shù) 此方法將參數(shù)的實(shí)際值復(fù)制到該函數(shù)的形參。在這種情況下,對該參數(shù)在函數(shù)內(nèi)部所做的更改沒有對參數(shù)產(chǎn)生影響。
引用參數(shù) 此方法將對實(shí)參的內(nèi)存位置的引用復(fù)制到形參。這意味著對參數(shù)所做的更改會(huì)影響參數(shù)本身。
輸出參數(shù) 這種方法有助于返回多個(gè)值。
上一篇:多態(tài)性下一篇:類型轉(zhuǎn)換