C# 命令行参数
C# 命令行参数
通过命令行传递的参数称为命令行参数。我们可以在执行代码时向 Main 方法发送参数。字符串 args 变量包含从命令行传递的所有值。
在以下示例中,我们在程序执行期间传递命令行参数。
C# 命令行参数示例
using System;
namespace CSharpProgram
{
class Program
{
// Main function, execution entry point of the program
static void Main(string[] args) // string type parameters
{
// Command line arguments
Console.WriteLine("Argument length: "+args.Length);
Console.WriteLine("Supplied Arguments are:");
foreach (Object obj in args)
{
Console.WriteLine(obj);
}
}
}
}
使用以下命令编译并执行该程序。
编译: csc Program.cs
执行: Program.exe 嗨,你好吗?
执行代码后,它会向控制台输出以下输出。
输出:
Argument length: 5
Supplied Arguments are:
Hi
there,
how
are
you?