最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

淺談Main方法的參數(shù)

 更新時(shí)間:2016年12月22日 08:57:54   作者:反骨仔(二五仔)  
本文主要對(duì)Main方法的參數(shù)通過案例分析進(jìn)行介紹,具有很好的參考價(jià)值,需要的朋友一起來看下吧

通過以下方式之一定義方法,可以將參數(shù)發(fā)送至 Main 方法。

static int Main(string[] args)

static void Main(string[] args)

【備注】若要在 Windows 窗體應(yīng)用程序中的 Main 方法中啟用命令行參數(shù),必須手動(dòng)修改 program.cs 中 Main 的簽名。 Windows 窗體設(shè)計(jì)器生成的代碼創(chuàng)建沒有輸入?yún)?shù)的 Main。 也可以使 用 Environment.CommandLine 或 Environment.GetCommandLineArgs 從控制臺(tái)或 Windows 應(yīng)用程序中的任何位置訪問命令行參數(shù)。

 Main 方法的參數(shù)是表示命令行參數(shù)的 String 數(shù)組。 一般是通過測(cè)試 Length 屬性來確定參數(shù)是否存在,例如:

  if (args.Length == 0)
  {
   WriteLine("Hello World.");
   return 1;
  } 

還可以使用 Convert 類或 Parse 方法將字符串參數(shù)轉(zhuǎn)換為數(shù)值類型。 例如,下面的語(yǔ)句使用 Parse 方法將 string 轉(zhuǎn)換為 long 數(shù)字:

long num = Int64.Parse(args[0]);  

也可以使用別名為 Int64 的 C# 類型 long:

long num = long.Parse(args[0]);  

還可以使用 Convert 類的方法 ToInt64 完成同樣的工作:

long num = Convert.ToInt64(s);  

示例 

下面的示例演示如何在控制臺(tái)應(yīng)用程序中使用命令行參數(shù)。 應(yīng)用程序在運(yùn)行時(shí)采用一個(gè)參數(shù),將該參數(shù)轉(zhuǎn)換為整數(shù),并計(jì)算該數(shù)的階乘。 如果沒有提供參數(shù),則應(yīng)用程序發(fā)出一條消息來解釋程序的正確用法。

public class Functions
 {
 public static long Factorial(int n)
 {
 if ((n < 0) || (n > 20))
 {
 return -1;
 }
 long tempResult = 1;
 for (int i = 1; i <= n; i++)
 {
 tempResult *= i;
 }
 return tempResult;
 }
 }
 class MainClass
 {
 static int Main(string[] args)
 {
 // Test if input arguments were supplied:
 if (args.Length == 0)
 {
 Console.WriteLine("Please enter a numeric argument.");
 Console.WriteLine("Usage: Factorial <num>");
 return 1;
 }
 int num;
 bool test = int.TryParse(args[0], out num);
 if (test == false)
 {
 Console.WriteLine("Please enter a numeric argument.");
 Console.WriteLine("Usage: Factorial <num>");
 return 1;
 }
 long result = Functions.Factorial(num);
 if (result == -1)
 Console.WriteLine("Input must be >= 0 and <= 20.");
 else
 Console.WriteLine("The Factorial of {0} is {1}.", num, result);

 return 0;
 }
 }

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!

相關(guān)文章

最新評(píng)論

承德市| 石阡县| 宁阳县| 贵港市| 贵德县| 蒙山县| 高雄市| 襄汾县| 社旗县| 望城县| 博湖县| 台安县| 铁岭市| 内黄县| 肥乡县| 河东区| 天长市| 东海县| 浦北县| 全椒县| 屏东县| 阜阳市| 潞城市| 观塘区| 犍为县| 婺源县| 尚志市| 札达县| 乐至县| 蕲春县| 滁州市| 荥经县| 三门峡市| SHOW| 镇巴县| 南丰县| 科尔| 长宁区| 广汉市| 东辽县| 泽库县|