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.cs360 chars17 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.cs25 chars2 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