如何在 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 输出。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP