In the previous "Hello World" DevCast, we created a program which was able to process arguments from the command-line. This time, we will review how that was done and discover another method to access the array of command-line parameters.
I have created a solution called "CommandLine" and two projects: one, called "CmdLine1"; and the other, named "CmdLine2".
For "CmdLine1", we will write something very similar to the "HelloWorld3" project from last time. We start by declaring that we will use the system namespace, create a class which will be called "CommandLine", and add the required entry method which, as we learned from "Hello World", must be declared with the keyword "static" and be called "Main" with a capital "M".
The "Main" method is the first method called when our program is started and that becomes obvious when we see how to access command-line arguments. Looking back at "HelloWorld3", you see that we type the program's name and then any parameters we want to send to the program. When we push enter, the console puts all of the parameters into an array and then executes the program by calling the "Main" method, using that parameter array as an an argument of the method.
We access the parameter array by defining the "Main" method such that it will accept an array argument. C# is a stongly typed language so each variable must be declared along with an associated type. In the case of the parameter array, we need to declare the variable "args" to be an array-type containing string-type elements. Square braces imediately after a type declaration tell the compiler that this will be an array containing elements of only that type.
If you have watched the "HelloWorld" DevCast, you will already be familiar with how we access the elements of the "args" array and properties of the array itself. I will paste in some code very similar to that example. One thing to note is that Length is a read-only property of the "args" array. You can see that in the tool-tip where it only mentions that it "gets" a 32-bit integer value. If we could modify the value, the tool-tip would mention that we could also "set" the value. Since "Length" cannot be set by the programmer, it means we cannot change the size of the array after it has been created.
The biggest difference between this example and the previous "HelloWorld3", is the string formatting with multiple arguments. The "zero in curly braces" will be replaced by the value of the argument "i" and the "one in curly braces" will be replaced by the value of the argument "args[i]", or, in other words, the element of the "args" array at position "i".
For the second project, I'll copy the first code-file because the only big difference is going to be how we loop through the "args" array. Change the class name to "CommandLine2" and then take a look at the "for-loop".
A "for-loop" is generally used when you know where you want to start and where you want to end. In this example, we want to start at the first element of the array and continue until the last element of the array. Arrays begin at index 0 and end at their length minus 1 so we start the "for-loop" counter at 0 and add one until until the counter counter is equal to the length of the array.
Since looping over an entire array is so common in programming, C# has a "foreach" loop to make things easier. Simply tell it to put each element in the array into a variable of the same type. Each element in the "args" array is of the string type so we write "foreach(string s in args)" where "s" can be any name that hasn't been used yet.
Now, each element of the "args" array will be sequentially put into "s" so we can write each of them to the console with "Console.WriteLine(s)".
When we execute this program from the console, you can see it runs with the same result as the previous "HelloWorld3" example.
I'm Justin Mancinelli, thanks for watching.
Links:
http://msdn.microsoft.com/en-au/library/cs8hbt1w.aspx
http://zpjdevcast.blogspot.com/