国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

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

2020-01-24 01:41:56
字體:
來源:轉載
供稿:網友

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

●引用參數:當使用引用參數的時候,將不會分配一個新的存儲位置,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]);      }    }  }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 浙江省| 香港| 三都| 当涂县| 安岳县| 石阡县| 永修县| 东乌珠穆沁旗| 永宁县| 卓尼县| 凤城市| 化州市| 剑河县| 靖边县| 巨野县| 永宁县| 南汇区| 布尔津县| 纳雍县| 永城市| 宁南县| 五家渠市| 从化市| 九龙坡区| 盘山县| 永靖县| 金昌市| 石楼县| 信宜市| 新安县| 南召县| 莱阳市| 辉南县| 新平| 彩票| 怀集县| 华蓥市| 霍城县| 偏关县| 灌南县| 胶州市|