Читання аргументів командного рядка

C#:
Читання аргументів командного рядка

How to: (Як це зробити:)

Here’s a simple code snippet for grabbing command line arguments in C#. Compile, run it via the console with some arguments, and see what happens.

using System;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Arguments count: " + args.Length);
        for (int i = 0; i < args.Length; i++)
        {
            Console.WriteLine($"Argument[{i}]: {args[i]}");
        }
    }
}

If you run myapp.exe arg1 arg2 arg3, expect output like this:

Arguments count: 3
Argument[0]: arg1
Argument[1]: arg2
Argument[2]: arg3

Deep Dive (Поглиблений Розділ):

Historically, command line arguments stem from the early days of computing when GUIs were a luxury. They’re all about efficiency; why click through dialogs when a simple text command does the trick?

Alternatives exist, like reading from a config file or a database, but command line arguments are unbeatable for simplicity. Sometimes you’ll use libraries like CommandLineParser for fancier argument parsing.

The Main method’s args parameter is an array of strings, each element holding an argument. In .NET 5 and onward, you can also use top-level statements, making things even more succinct, though the principle remains the same.

See Also (Дивіться також):