application parameter in csharp

In C#, command-line arguments can be passed to an application using the Main method which takes a string array as its parameter. This array contains the arguments passed to the application via the command-line.

Here is an example of how to retrieve the command-line arguments:

main.cs
static void Main(string[] args)
{
    // Check if any arguments were passed
    if (args.Length > 0)
    {
        // Iterate through the arguments and display them
        foreach (string arg in args)
        {
            Console.WriteLine("Argument: {0}", arg);
        }
    }
    else
    {
        Console.WriteLine("No arguments were passed.");
    }
}
360 chars
17 lines

In the example above, args.Length is used to check if any arguments were passed to the application. If there are arguments, we loop through each argument in args and display them using Console.WriteLine(). If no arguments were passed, we display a message indicating that no arguments were passed.

To pass arguments to an application from the command-line, simply include them after the application name when running the application. For example:

main.cs
myapp.exe arg1 arg2 arg3
25 chars
2 lines

In this example, myapp.exe is the name of the application and arg1, arg2, and arg3 are the arguments passed to the application.

gistlibby LogSnag