Visual C# How to Program
Authors: Deitel H. M.
Published year: 2004
Pages: 162-163/600
Buy this book on amazon.com >>

[Page 375 ( continued )]

8.12. Variable-Length Argument Lists

Variable-length argument lists allow you to create methods that receive an arbitrary number of arguments. A one-dimensional array-type argument preceded by the keyword params in a method's parameter list indicates that the method receives a variable number of arguments with the type of the array's elements. This use of a params modifier can occur only in the last entry of the parameter list. While you can use method overloading and array passing to accomplish much of what is accomplished with " varargs "another name for variable-length argument listsusing the params modifier is more concise .

Figure 8.22 demonstrates method Average (lines 817), which receives a variable-length sequence of double s (line 8). C# treats the variable-length argument list as a one-dimensional array whose elements are all of the same type. Hence, the method body can manipulate the parameter numbers as an array of double s. Lines 1314 use the foreach loop to walk through the array and calculate the total of the double s in the array. Line 16 accesses numbers.Length to obtain the size of the numbers array for use in the averaging calculation. Lines 31, 33 and 35 in Main call method Average with two, three and four arguments, respectively. Method Average has a variable-length argument list, so it can average as many double arguments as the caller passes . The output reveals that each call to method Average returns the correct value.


[Page 376]
Figure 8.22. Using variable-length argument lists.

1



// Fig. 8.22: VarargsTest.cs



2



// Using variable-length argument lists.



3



using


System;

4


5



public class


VarargsTest

6

{

7



// calculate average



8



public static double


Average(



params double


[] numbers

)

9

{

10



double


total =


0.0


;


// initialize total



11


12




// calculate total using the foreach statement




13




foreach


(


double


d


in


numbers )


14


total += d;


15


16



return


total / numbers.Length;

17

}


// end method Average



18


19



public static void


Main(


string


[] args )

20

{

21



double


d1 =


10.0


;

22



double


d2 =


20.0


;

23



double


d3 =


30.0


;

24



double


d4 =


40.0


;

25


26

Console.WriteLine(

27



"d1 = {0:F1}\nd2 = {1:F1}\nd3 = {2:F1}\nd4 = {3:F1}\n"


,

28

d1, d2, d3, d4 );

29


30

Console.WriteLine(


"Average of d1 and d2 is {0:F1}"


,

31


Average( d1, d2 )

);

32

Console.WriteLine(


"Average of d1, d2 and d3 is {0:F1}"


,

33


Average( d1, d2, d3 )

);

34

Console.WriteLine(


"Average of d1, d2, d3 and d4 is {0:F1}"


,

35


Average( d1, d2, d3, d4 )

);

36

}


// end Main



37

}


// end class VarargsTest



d1 = 10.0
 d2 = 20.0
 d3 = 30.0
 d4 = 40.0

 Average of d1 and d2 is 15.0
 Average of d1, d2 and d3 is 20.0
 Average of d1, d2, d3 and d4 is 25.0

Common Programming Error 8.4

Using the params modifier with a parameter in the middle of a method parameter list is a syntax error. The params modifier may be used only with the last parameter of the parameter list.




[Page 377]

8.13. Using Command-Line Arguments

On many systems, it is possible to pass arguments from the command line (these are known as command-line arguments ) to an application by including a parameter of type string[] (i.e., an array of string s) in the parameter list of Main , exactly as we have done in every application in the book. By convention, this parameter is named args (Fig. 8.23, line 7). When an application is executed from the Command Prompt, the execution environment passes the command-line arguments that appear after the application name to the application's Main method as string s in the one-dimensional array args . The number of arguments passed from the command line is obtained by accessing the array's Length property. For example, the command "MyApplication a b" passes two command-line arguments to application MyApplication . Note that command-line arguments are separated by white space, not commas. When the preceding command executes, the Main method entry point receives the two-element array args (i.e., args.Length is 2 ) in which args[ 0 ] contains the string "a" and args[ 1 ] contains the string "b" . Common uses of command-line arguments include passing options and file names to applications.

Figure 8.23. Using command-line arguments to initialize an array.

1



// Fig. 8.23: InitArray.cs



2



// Using command-line arguments to initialize an array.



3



using


System;

4


5



public class


InitArray

6

{

7



public static void


Main(


string


[] args )

8

{

9



// check number of command-line arguments



10



if


(

args.Length !=


3



)

11

Console.WriteLine(

12



"Error: Please re-enter the entire command, including\n"


+

13



"an array

size

, initial value and increment."


);

14



else



15

{

16



// get array size from first command-line argument



17




int


arrayLength = Convert.ToInt32( args[




] );


18



int


[] array =


new int


[ arrayLength ];


// create array



19


20



// get initial value and increment from command-line argument



21




int


initialValue = Convert.ToInt32( args[


1


] );


22




int


increment = Convert.ToInt32( args[


2


] );


23


24




// calculate value for each array element




25




for


(


int


counter =




; counter < array.Length; counter++ )


26


array[ counter ] = initialValue + increment * counter;


27


28

Console.WriteLine(


"{1,8}"


,


"Index"


,


"Value"


);

29


[Page 378]

30



// display array index and value



31



for


(


int


counter =




; counter < array.Length; counter++ )

32

Console.WriteLine(


"{0,5}{1,8}"


, counter, array[ counter ] );

33

}


// end else



34

}


// end Main



35

}


// end class InitArray



C:\Examples\ch08\fig08_21>


InitArray.exe


Error: Please re-enter the entire command, including an array size, initial value and increment.

C:\Examples\ch08\fig08_21>


InitArray.exe 5 0 4


Index Value 0 0 1 4 2 8 3 12 4 16

C:\Examples\ch08\fig08_21>


InitArray.exe 10 1 2


Index Value 0 1 1 3 2 5 3 7 4 9 5 11 6 13 7 15 8 17 9 19

Figure 8.23 uses three command-line arguments to initialize an array. When the application executes, if args.Length is not 3 , the application prints an error message and terminates (lines 1013). Otherwise, lines 1632 initialize and display the array based on the values of the command-line arguments.


[Page 378]

The command-line arguments become available to Main as string s in args . Line 17 gets args[ 0 ] a string that specifies the array sizeand converts it to an int value, which the application uses to create the array in line 18. The static method ToInt32 of class Convert converts its string argument to an int .

Lines 2122 convert the args[ 1 ] and args[ 2 ] command-line arguments to int values and store them in initialValue and increment , respectively. Lines 2526 calculate the value for each array element.

The output of the first sample execution indicates that the application received an insufficient number of command-line arguments. The second sample execution uses command-line arguments 5 , and 4 to specify the size of the array ( 5 ), the value of the first element ( ) and the increment of each value in the array ( 4 ), respectively. The corresponding output indicates that these values create an array containing the integers , 4 , 8 , 12 and 16 . The output from the third sample execution illustrates that the command-line arguments 10 , 1 and 2 produce an array whose 10 elements are the nonnegative odd integers from 1 to 19 .


Visual C# How to Program
Authors: Deitel H. M.
Published year: 2004
Pages: 162-163/600
Buy this book on amazon.com >>

Similar books on Amazon