在 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