C# 的 main 方法中如何传递命令行参数?

来源:这里教程网 时间:2026-02-21 16:35:17 作者:

c# 的 main 方法中如何传递命令行参数?

Main() 方法是入口点 -

static void Main(string[] args)

参数数组args用于设置参数 -

string[] args)

如果添加两个参数,它将设置以下内容 -

var args = new string[] {"arg1","arg2”}

这里是演示代码 -

示例

using System;
namespace Demo {
   class HelloWorld {
      // args for command line
      static void Main(string[] args) {
         Console.WriteLine("Welcome here!");
         Console.ReadKey();
      }
   }
}

使用命令行而不是 Visual Studio IDE 编译 C# 程序 -

打开文本编辑器并添加上述代码.

将文件另存为 helloworld.cs

打开命令提示符工具并转到保存文件的目录文件。

输入 csc helloworld.cs 并按 Enter 编译您的代码。

如果您的代码中没有错误代码后,命令提示符会将您带到下一行并生成 helloworld.exe 可执行文件。

键入 helloworld 来执行您的程序。

您可以看到输出 Hello World 打印在屏幕上。

相关推荐