C# 中的命令列引數


如果你想透過命令列傳遞引數,請在 C# 中使用命令列引數 −

當我們使用 c# 建立程式時,會使用 static void main,並且可以在其中檢視引數。

class HelloWorld {
   static void Main(string[] args) {
      /* my first program in C# */
      Console.WriteLine("Hello World");
      Console.ReadKey();
   }

string[] args 是一個變數,它擁有從命令列中傳遞來的所有值,如上所示。

現在想要列印這些引數,我們假設有一個引數“One” −

Console.WriteLine("Length of the arguments: "+args.Length);
Console.WriteLine("Arguments:");
foreach (Object obj in args) {
   Console.WriteLine(obj);
}

以上操作將列印 −

Length of the arguments: 1
Arguments: One

更新時間:20-Jun-2020

3 千+ 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.