C#基本語法:辦法參數詳解。本站提示廣大學習愛好者:(C#基本語法:辦法參數詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是C#基本語法:辦法參數詳解正文
●值參數 :一個值參數相當於一個部分變量,當應用值參數的時刻,將會分派一個新的存儲地位,將實參拷貝到該地位,並將該拷貝值傳遞給該辦法。是以,值參數只能將值帶進辦法,然則不克不及帶出辦法,而不會影響實參的值。
●援用參數:當應用援用參數的時刻,將不會分派一個新的存儲地位,In other words,援用參數能將值帶進辦法,也能帶出辦法,因此會影響實參的值。以下例:
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(); } } }
●輸入參數:依據表層寄義猜想其只能輸入不克不及輸出辦法的參數,我們開端緊隨上例驗證一下,參加以下代碼:
static void Transposition_2(ref int a,ref int b) { int temp = a; a = b; b = temp; }
編譯器便會提示a,b未賦值的報錯,異樣我們也便可以直不雅的看到,輸入參數不克不及將值帶進辦法,只能將值輸入辦法。從上面的例子中可以看出在辦法的外部停止了輸入參數的賦值操作,是以不管在哪裡應用輸入參數都必需提早賦值,這也是應用任何類型參數的個性。
//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[,]),可認為參數數組制訂一個或多個實參,個中每個實參都是一個表達式,另外參數數組和統一類型的值參數完整等效。例以下例:
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();
也不知咋弄的,我的輸出法和編譯器似乎在玩躲貓貓,一會紛歧會的就不支撐漢字輸出了,我也真能用英語輸出了,無法。
上面是這一日記的參考源碼,可以全體剖析一下:
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]); } } } }