在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

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

方法

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

  • 定義一個方法
  • 調用方法

在 C# 中定義方法

當你定義一個方法時,你基本上要聲明其結構的組成元素。在 C# 中定義方法的語法如下所示:

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

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

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

示例
下面的代碼段顯示了一個函數 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();
      }
   }
}

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

Max value is : 200

你也可以通過使用類的實例來從其他類中調用公開方法。
例如,FindMax 它屬于 NumberManipulator 類中的方法,你可以從另一個類測試中調用它。

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í)行上述代碼,得到如下結果:

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
         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í)行上述代碼,得到如下結果:

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

將參數傳遞給方法

當調用帶參數的方法時,您需要將參數傳遞給該方法。有三種方法,可以將參數傳遞給方法:

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