日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
如何將參數(shù)上傳到Main方法-創(chuàng)新互聯(lián)

如何將參數(shù)上傳到Main方法,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

創(chuàng)新互聯(lián)專注于云巖網(wǎng)站建設服務及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供云巖營銷型網(wǎng)站建設,云巖網(wǎng)站制作、云巖網(wǎng)頁設計、云巖網(wǎng)站官網(wǎng)定制、小程序制作服務,打造云巖網(wǎng)絡公司原創(chuàng)品牌,更為您提供云巖網(wǎng)站排名全網(wǎng)營銷落地服務。

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

static int Main(string[] args)

static void Main(string[] args)

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

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

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

還可以使用 Convert 類或 Parse 方法將字符串參數(shù)轉(zhuǎn)換為數(shù)值類型。 例如,下面的語句使用 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);  


示例 

下面的示例演示如何在控制臺應用程序中使用命令行參數(shù)。 應用程序在運行時采用一個參數(shù),將該參數(shù)轉(zhuǎn)換為整數(shù),并計算該數(shù)的階乘。 如果沒有提供參數(shù),則應用程序發(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 ");
 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 ");
 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;
 }
 }

看完上述內(nèi)容,你們掌握如何將參數(shù)上傳到Main方法的方法了嗎?如果還想學到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!


當前文章:如何將參數(shù)上傳到Main方法-創(chuàng)新互聯(lián)
URL網(wǎng)址:http://www.dlmjj.cn/article/dchpdg.html