C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Also: You can use a Main method that returns an int or that receives no parameters if you want to. Main() can be defined in any class.
ReturnInt, uintNext: This program shows how the command-line parameters are received from a Windows command line.
Step 1: Upon startup the Main method is executed. It then tests for a null array argument.
Step 2: We print the Length of the arguments, and then loop over them. We write the arguments with Console.WriteLine.
ConsoleC# program that receives command-line arguments
using System;
class Program
{
static void Main(string[] args)
{
// Step 1: test for null.
if (args == null)
{
Console.WriteLine("args is null");
}
else
{
// Step 2: print length, and loop over all arguments.
Console.Write("args length is ");
Console.WriteLine(args.Length);
for (int i = 0; i < args.Length; i++)
{
string argument = args[i];
Console.Write("args index ");
Console.Write(i); // Write index
Console.Write(" is [");
Console.Write(argument); // Write string
Console.WriteLine("]");
}
}
Console.ReadLine();
}
}
Output
"C:\ConsoleApplication1.exe" a b c
args length is 3
args index 0 is [a]
args index 1 is [b]
args index 2 is [c]
"C:\ConsoleApplication1.exe" a b c
args length is 3
args index 0 is [a]
args index 1 is [b]
args index 2 is [c]
"C:\\ConsoleApplication1.exe" http://www.dotnetCodex.com/
args length is 1
args index 0 is [http://www.dotnetCodex.com/]
"C:\ConsoleApplication1.exe" "Literal test "
args length is 1
args index 0 is [Literal test ]
But: If you want to preserve whitespace on the command line, use the quotation marks around your parameter.
Equivalent command lines:
"C:\ConsoleApplication1.exe" a b c
"C:\ConsoleApplication1.exe" a b c
Return int: You can have the Main method return an int. This tells the operating system the program's result.
Tip: It is also useful to omit the string[] args parameter array entirely when not needed.
C# program that returns int from Main
class Program
{
static int Main()
{
// We can return an int from a program exe.
System.Console.WriteLine("[DONE]");
return 600;
}
}
Output
[DONE]
Foreach: The foreach-loop is a good choice when the index of each element is not important to its usage.
ForeachFor: When we need to do something special with element 1 or element 3, the index is important, so it is better to use a for-loop.
ForC# program that uses args loops
using System;
class Program
{
static void Main(string[] args)
{
// The program control flow begins here.
foreach (string value in args)
{
Console.WriteLine("foreach: {0}", value);
}
for (int i = 0; i < args.Length; i++)
{
string value = args[i];
Console.WriteLine("for: {0}", value);
}
Console.ReadLine();
}
}
Output
foreach: Arg1
foreach: Arg2
foreach: Arg3
for: Arg1
for: Arg2
for: Arg3
Then: Right-click on the EXE file and select Create Shortcut. Find the "Target" text box in the Shortcut Properties window.
Finally: Append the command-line arguments after the file name in the Target text box.
Result: When you click on the shortcut, these strings will be put into the string[] args array at runtime in the Main method.
Arguments from screenshot:
a b
However: If incorrect parameters are likely, it is best to wrap Main() with try, catch and finally blocks.
TryCatchFinallyTip: This ensures the best possible execution paths are taken. If a step is critical, put it in a finally block.