CHAPTER 3 C# LANGUAGE FUNDAMENTALS 67 Figure (Web design tools)
CHAPTER 3 C# LANGUAGE FUNDAMENTALS 67 Figure 3-1. Supplying arguments at the command line Processing Command-Line Arguments Assume that you now wish to update HelloClass to process possible command-line parameters: // This time, check if you have been sent any command-line arguments. using System; class HelloClass { public static int Main(string[] args) { Console.WriteLine(”***** Command line args *****”); for(int i = 0; i < args.Length; i++) Console.WriteLine("Arg: {0} ", args[i]); ... } } Here, you are checking to see if the array of strings contains some number of items using the Length property of System.Array (as you ll see later in this chapter, all C# arrays actually alias the System.Array type, and therefore have a common set of members). As you loop over each item in the array, its value is printed to the console window. Supplying the arguments at the command line is equally as simple, as shown in Figure 3-1. As an alternative to the standard for loop, you may iterate over incoming string arrays using the C# foreach keyword. This bit of syntax is fully explained later in this chapter, but here is some sample usage: // Notice you have no need to check the size of the array when using 'foreach'. public static int Main(string[] args) { ... foreach(string s in args) Console.WriteLine("Arg: {0} ", s); ... } Finally, you are also able to access command-line arguments using the static GetCommand- LineArgs() method of the System.Environment type. The return value of this method is an array of strings. The first index identifies the current directory containing the application itself, while the remaining elements in the array contain the individual command-line arguments (when using this technique, you no longer need to define the Main() method as taking a string array parameter):
Searching for affordable and reliable webhost to host and run your web applications? Go to our java web server services and you will be pleased.