中文字幕一区二区人妻电影,亚洲av无码一区二区乱子伦as ,亚洲精品无码永久在线观看,亚洲成aⅴ人片久青草影院按摩,亚洲黑人巨大videos

C# 方法

一個方法是把一些相關(guān)的語句組織在一起,用來執(zhí)行一個任務(wù)的語句塊。每一個 C# 程序至少有一個帶有 Main 方法的類。

要使用一個方法,您需要:

  • 定義方法
  • 調(diào)用方法

C# 中定義方法

當(dāng)定義一個方法時,從根本上說是在聲明它的結(jié)構(gòu)的元素。在 C# 中,定義方法的語法如下:

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

下面是方法的各個元素:

  • Access Specifier:訪問修飾符,這個決定了變量或方法對于另一個類的可見性。
  • Return type:返回類型,一個方法可以返回一個值。返回類型是方法返回的值的數(shù)據(jù)類型。如果方法不返回任何值,則返回類型為 void。
  • Method name:方法名稱,是一個唯一的標(biāo)識符,且是大小寫敏感的。它不能與類中聲明的其他標(biāo)識符相同。
  • Parameter list:參數(shù)列表,使用圓括號括起來,該參數(shù)是用來傳遞和接收方法的數(shù)據(jù)。參數(shù)列表是指方法的參數(shù)類型、順序和數(shù)量。參數(shù)是可選的,也就是說,一個方法可能不包含參數(shù)。
  • Method body:方法主體,包含了完成任務(wù)所需的指令集。

實(shí)例

下面的代碼片段顯示一個函數(shù) FindMax,它接受兩個整數(shù)值,并返回兩個中的較大值。它有 public 訪問修飾符,所以它可以使用類的實(shí)例從類的外部進(jìn)行訪問。

實(shí)例

class NumberManipulator
{
? ?public int FindMax(int num1, int num2)
? ?{
? ? ? /* 局部變量聲明 */
? ? ? int result;

? ? ? if (num1 > num2)
? ? ? ? ?result = num1;
? ? ? else
? ? ? ? ?result = num2;

? ? ? return result;
? ?}
? ?...
}

C# 中調(diào)用方法

您可以使用方法名調(diào)用方法。下面的實(shí)例演示了這點(diǎn):

實(shí)例

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í)行時,它會產(chǎn)生下列結(jié)果:

最大值是: 200

您也可以使用類的實(shí)例從另一個類中調(diào)用其他類的公有方法。例如,方法 FindMax 屬于 NumberManipulator 類,您可以從另一個類 Test 中調(diào)用它。

實(shí)例

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í)行時,它會產(chǎn)生下列結(jié)果:

最大值是: 200

遞歸方法調(diào)用

一個方法可以自我調(diào)用。這就是所謂的 遞歸。下面的實(shí)例使用遞歸函數(shù)計算一個數(shù)的階乘:

實(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í)行時,它會產(chǎn)生下列結(jié)果:

6 的階乘是: 720
7 的階乘是: 5040
8 的階乘是: 40320

參數(shù)傳遞

當(dāng)調(diào)用帶有參數(shù)的方法時,您需要向方法傳遞參數(shù)。在 C# 中,有三種向方法傳遞參數(shù)的方式:

方式描述
值參數(shù)這種方式復(fù)制參數(shù)的實(shí)際值給函數(shù)的形式參數(shù),實(shí)參和形參使用的是兩個不同內(nèi)存中的值。在這種情況下,當(dāng)形參的值發(fā)生改變時,不會影響實(shí)參的值,從而保證了實(shí)參數(shù)據(jù)的安全。
引用參數(shù)這種方式復(fù)制參數(shù)的內(nèi)存位置的引用給形式參數(shù)。這意味著,當(dāng)形參的值發(fā)生改變時,同時也改變實(shí)參的值。
輸出參數(shù)這種方式可以返回多個值。

按值傳遞參數(shù)

這是參數(shù)傳遞的默認(rèn)方式。在這種方式下,當(dāng)調(diào)用一個方法時,會為每個值參數(shù)創(chuàng)建一個新的存儲位置。

實(shí)際參數(shù)的值會復(fù)制給形參,實(shí)參和形參使用的是兩個不同內(nèi)存中的值。所以,當(dāng)形參的值發(fā)生改變時,不會影響實(shí)參的值,從而保證了實(shí)參數(shù)據(jù)的安全。下面的實(shí)例演示了這個概念:

實(shí)例

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í)行時,它會產(chǎn)生下列結(jié)果:

在交換之前,a 的值:100
在交換之前,b 的值:200
在交換之后,a 的值:100
在交換之后,b 的值:200

結(jié)果表明,即使在函數(shù)內(nèi)改變了值,值也沒有發(fā)生任何的變化。

按引用傳遞參數(shù)

引用參數(shù)是一個對變量的內(nèi)存位置的引用。當(dāng)按引用傳遞參數(shù)時,與值參數(shù)不同的是,它不會為這些參數(shù)創(chuàng)建一個新的存儲位置。引用參數(shù)表示與提供給方法的實(shí)際參數(shù)具有相同的內(nèi)存位置。

在 C# 中,使用 ref 關(guān)鍵字聲明引用參數(shù)。下面的實(shí)例演示了這點(diǎn):

實(shí)例

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í)行時,它會產(chǎn)生下列結(jié)果:

在交換之前,a 的值:100
在交換之前,b 的值:200
在交換之后,a 的值:200
在交換之后,b 的值:100

結(jié)果表明,swap 函數(shù)內(nèi)的值改變了,且這個改變可以在 Main 函數(shù)中反映出來。

按輸出傳遞參數(shù)

return 語句可用于只從函數(shù)中返回一個值。但是,可以使用 輸出參數(shù) 來從函數(shù)中返回兩個值。輸出參數(shù)會把方法輸出的數(shù)據(jù)賦給自己,其他方面與引用參數(shù)相似。

下面的實(shí)例演示了這點(diǎn):

實(shí)例

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í)行時,它會產(chǎn)生下列結(jié)果:

在方法調(diào)用之前,a 的值: 100
在方法調(diào)用之后,a 的值: 5

提供給輸出參數(shù)的變量不需要賦值。當(dāng)需要從一個參數(shù)沒有指定初始值的方法中返回值時,輸出參數(shù)特別有用。請看下面的實(shí)例,來理解這一點(diǎn):

實(shí)例

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;
? ? ? ? ?
? ? ? ? ?/* 調(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í)行時,它會產(chǎn)生下列結(jié)果(取決于用戶輸入):

請輸入第一個值:
7
請輸入第二個值:
8
在方法調(diào)用之后,a 的值: 7
在方法調(diào)用之后,b 的值: 8