C#基礎文法:方法參數詳解

來源:互聯網
上載者:User

C#基礎文法:方法參數詳解
這篇文章主要介紹了C#基礎文法:方法參數詳解,本文講解了值參數、引用參數、輸出參數、參數數組等參數類型,並分別給出代碼執行個體,需要的朋友可以參考下

 

 

●值參數 :一個值參數相當於一個局部變數,當使用值參數的時候,將會分配一個新的儲存位置,將實參拷貝到該位置,並將該拷貝值傳遞給該方法。因此,值參數只能將值帶進方法,但是不能帶出方法,而不會影響實參的值。

●引用參數:當使用引用參數的時候,將不會分配一個新的儲存位置,In other words,引用參數能將值帶進方法,也能帶出方法,因而會影響實參的值。如下例:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

using System;

 

namespace prg1

{

class Paramstest

{

//值參數使用示範

public static void Transposition_1(int a, int b)

{

int temp = a;

a = b;

b = temp;

}

//引用參數使用示範

static void Transposition_2(ref int a,ref int b)

{

int temp = a;

a = b;

b = temp;

}

static void Main(string[] args)

{

int a = 25;

int b = 30;

//調用Transposition_1

Console.WriteLine("調用Transposition_1之前a={0},b={1}",a,b);

Transposition_1(a,b);

Console.WriteLine("調用Transposition_1之後a={0},b={1}", a, b);

Console.WriteLine("====================\n");

//調用Transposition_2

Console.WriteLine("調用Transposition_2之前a={0},b={1}", a, b);

Transposition_2(ref a,ref b);

Console.WriteLine("調用Transposition_2之後a={0},b={1}", a, b);

Console.WriteLine("====================\n");

Console.ReadKey();

}

}

}


●輸出參數:根據表層含義猜測其只能輸出不能輸入方法的參數,我們開始緊隨上例驗證一下,加入以下代碼:

?

1

2

3

4

5

6

static void Transposition_2(ref int a,ref int b)

{

int temp = a;

a = b;

b = temp;

}

編譯器便會提醒a,b未賦值的報錯,同樣我們也就可以直觀的看到,輸出參數不能將值帶進方法,只能將值輸出方法。從下面的例子中可以看出在方法的內部進行了輸出參數的賦值操作,因此無論在哪裡使用輸出參數都必須提前賦值,這也是使用任何型別參數的共性。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

//Use of output parameters

static void Transposition_3(string name,out string FistName,out string LastName)

{

int i=name.Length;//Get the length of the string

while(i>0)

{

char chparm=name[i-1];

if (chparm == '.')

{

break;

}

i--;

}

FistName = name.Substring(0,i-1);

LastName = name.Substring(i);

 

}

//調用Transposition_3

string DoName,Nmark;

Transposition_3("rohelm.X",out DoName,out Nmark);

Console.WriteLine("Domain Name of Myself: {0}",DoName);

Console.WriteLine("The last name of my Domain Name: {0}",Nmark);

 


●參數數組:簡而言之,就是方法傳遞的單位是個數組,而且可以是一維數組或者交錯數組(形如int[][]),但是不能是多維陣列(形如;string[,]),可以為參數數組制定一個或多個實參,其中每一個實參都是一個運算式,此外參數數組和同一類型的值參數完全等效。例如下例:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

class Prmarry

{

public static void Show(params string[] name)

{

Console.WriteLine("Array contains the number of elements: {0}", name.Length);

Console.Write("elements of NameArray:");

for (int i = 0; i < name.Length; i++)

{

Console.Write("{0,10}",name[i]);

}

}

}

//調用Show

string[] NameArray = { "rohelm.X", "Boolean", "rrats" };

Prmarry.Show(NameArray);

Console.ReadKey();

也不知咋搞的,我的IME和編譯器好像在玩躲貓貓,一會不一會的就不支援漢字輸入了,我也真能用英語輸入了,無奈。

下面是這一日誌的參考源碼,可以整體分析一下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

using System;

 

namespace prg1

{

class Paramstest

{

//值參數使用示範

public static void Transposition_1(int a, int b)

{

int temp = a;

a = b;

b = temp;

}

//引用參數使用示範

static void Transposition_2(ref int a,ref int b)

{

int temp = a;

a = b;

b = temp;

}

//Use of output parameters

static void Transposition_3(string name,out string FistName,out string LastName)

{

int i=name.Length;//Get the length of the string

while(i>0)

{

char chparm=name[i-1];

if (chparm == '.')

{

break;

}

i--;

}

 

FistName = name.Substring(0, i - 1);

LastName = name.Substring(i);

}

static void Main(string[] args)

{

int a = 25;

int b = 30;

//調用Transposition_1

Console.WriteLine("調用Transposition_1之前a={0},b={1}",a,b);

Transposition_1(a,b);

Console.WriteLine("調用Transposition_1之後a={0},b={1}", a, b);

Console.WriteLine("====================\n");

//調用Transposition_2

Console.WriteLine("調用Transposition_2之前a={0},b={1}", a, b);

Transposition_2(ref a,ref b);

Console.WriteLine("調用Transposition_2之後a={0},b={1}", a, b);

Console.WriteLine("====================\n");

//調用Transposition_3

string DoName,Nmark;

Transposition_3("rohelm.X",out DoName,out Nmark);

Console.WriteLine("Domain Name of Myself: {0}",DoName);

Console.WriteLine("The last name of my Domain Name: {0}"+"\n",Nmark);

//調用Show

string[] NameArray = { "rohelm.X", "Boolean", "rrats" };

Prmarry.Show(NameArray);

Console.ReadKey();

}

}

class Prmarry

{

public static void Show(params string[] name)

{

Console.WriteLine("Array contains the number of elements: {0}", name.Length);

Console.Write("elements of NameArray:");

for (int i = 0; i < name.Length; i++)

{

Console.Write("{0,10}",name[i]);

}

}

}

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.