Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "string[] args" in Main class for?

Tags:

c#

In C# the Main class has string[] args parameter.

What is that for and where does it get used?

like image 442
Ali Avatar asked Feb 16 '09 09:02

Ali


People also ask

Why String [] is used in Java?

Strings are very special in Java, the content within the string cannot be changed or overridden after creation. Strings support many other functionalities, and programmers can use them according to their project requirement.

What is the difference between String arg [] and String [] args?

"String... args" will declare a method that expects a variable number of String arguments. The number of arguments can be anything at all: including zero. "String[] args" and the equivalent "String args[]" will declare a method that expects exactly one argument: an array of strings.

Why do we use String [] args in C#?

When we create a program in c#, static void main is used and we can see the arguments in it . The string[] args is a variable that has all the values passed from the command line as shown above.

Why is String args compulsory in Java?

It's a String because the command line is expressed in text. If you want to convert that text into integers or booleans, you have to do that yourself - how would the operating system or Java bootstrapper know exactly how you wanted everything to be parsed?


1 Answers

From the C# programming guide on MSDN:

The parameter of the Main method is a String array that represents the command-line arguments

So, if I had a program (MyApp.exe) like this:

class Program {   static void Main(string[] args)   {     foreach (var arg in args)     {       Console.WriteLine(arg);     }   } }

That I started at the command line like this:

MyApp.exe Arg1 Arg2 Arg3

The Main method would be passed an array that contained three strings: "Arg1", "Arg2", "Arg3".

If you need to pass an argument that contains a space then wrap it in quotes. For example:

MyApp.exe "Arg 1" "Arg 2" "Arg 3"

Command line arguments commonly get used when you need to pass information to your application at runtime. For example if you were writing a program that copies a file from one location to another you would probably pass the two locations as command line arguments. For example:

Copy.exe C:\file1.txt C:\file2.txt
like image 196
Daniel Richardson Avatar answered Oct 11 '22 16:10

Daniel Richardson