In C# the Main class has string[] args parameter.
What is that for and where does it get used?
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.
"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.
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.
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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With