●值參數 :一個值參數相當于一個局部變量,當使用值參數的時候,將會分配一個新的存儲位置,將實參拷貝到該位置,并將該拷貝值傳遞給該方法。因此,值參數只能將值帶進方法,但是不能帶出方法,而不會影響實參的值。
●引用參數:當使用引用參數的時候,將不會分配一個新的存儲位置,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]);      }    }  }}新聞熱點
疑難解答