The arguments which are passed by the user or programmer to the Main() method is termed as Command-Line Arguments. Main() method is the entry point of execution of a program. Main() method accepts array of strings. But it never accepts parameters from any other method in the program. In C# the command line arguments are passed to the Main() methods by stating as follows:
static void Main(string args) or static int Main(string args)
Note : To enable command-line arguments in the Main method in a Windows Forms Application, one must manually modify the signature of Main in program.cs. Windows Forms designer generates code and creates Main without an input parameter. One can also use Environment.CommandLine or Environment.GetCommandLineArgs to access the command-line arguments from any point in a console or Windows application.
To Compile and execute the above program follow below commands :
Compile: csc Geeks.cs Execute: Geeks.exe Welcome To GeeksforGeeks!
Arguments Passed by the Programmer: Welcome To GeeksforGeeks!
In above program Length is used to find the number of command line arguments and command line arguments will store in args array. With the help of Convert class and parse methods, one can also convert string argument to numeric types. For examples:
long num = Int64.Parse(args); or long num = long.Parse(args);
This will changes the string to a long type by using the Parse method. It is also possible to use the C# type long, which aliases Int64. Similarly one can parse using predefined parsing methods in C#.
- Passing arrays as arguments in C#
- Difference between Python and C#
- Difference between C and C#
- Writing to Excel Sheet Using EPPlus in C#
- Lambda Expressions in C#
- How to Get Started with Game Development?
- Basics of File Handling in C#
- C# | ToolTip Class
- C# | RichTextBox Class
- C# | MaskedTextBox Class
- C# | NumericUpDown Class
- C# | DateTimePicker Class
- C# | GroupBox Class
- C# | ListBox Class
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.