程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 更多關於編程 >> C#基礎語法:方法參數詳解

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

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

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

    ? 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]); } } } }
    1. 上一頁:
    2. 下一頁:
    Copyright © 程式師世界 All Rights Reserved