C#教程
C#控制语句
C#函数
C#数组
C#面向对象
C#命名空间和异常
C#文件IO
C#集合
C#多线程
C#其它

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?

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4