Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console app arguments, how arguments are passed to Main method

This would be question from c# beginner. When I create console application I get Main method with parameter args as array string. I do not understand how this method is called by system and how args are passed to the Main method. Maybe someone could explain? Is Main method are overridden of some kind of console class?

namespace ConsoleApplication1 {     class Program     {             static void Main(string[] args)         {             }     } } 
like image 231
Tomas Avatar asked May 27 '11 10:05

Tomas


People also ask

What is string [] args in main Method C#?

The string[] args is a variable that has all the values passed from the command line as shown above. Now to print those arguments, let's say we have an argument, “One” − Console. WriteLine("Length of the arguments: "+args. Length); Console. WriteLine("Arguments:"); foreach (Object obj in args) { Console.

Can main method have arguments C#?

Overloading of Main() method is allowed. But in that case, only one Main() method is considered as one entry point to start the execution of the program. The allowed type of command line arguments is only String array in the parameters of the Main() method.


1 Answers

All answers are awesome and explained everything very well

but I just want to point out different way for passing args to main method

in visual studio

  1. right click on Project then choose Properties
  2. go to Debug tab then on the Start Options section provide the app with your args

like this image

Properties window

and happy knowing secrets

like image 189
Basheer AL-MOMANI Avatar answered Oct 05 '22 07:10

Basheer AL-MOMANI