C# 中如何在 main 方法中传递命令行参数?
csharpprogrammingserver side programming更新于 2025/5/28 16:07:17
Main() 方法是入口点 −
static void Main(string[] args)
参数数组 args 用于设置参数 −
string[] args)
如果添加两个参数,它将设置以下内容 −
var args = new string[] {"arg1","arg2”}
以下是演示代码 −
示例
using System;
namespace Demo {
class HelloWorld {
// 命令行的参数
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。

