Parameter array (Params)

Source: Internet
Author: User

In the examples mentioned so far, the number of parameters is fixed by the method declaration. However, we sometimes want the number of parameters to be variable. Take the combine () method in code listing 4-11 as an example. In that method, the drive letter, folder path, file name (driveletter, folderpath, filename), and other parameters are passed. If the number of folders in the path is greater than 1, and the caller wants to link the extra folders to form a complete path, how should he write the code? Perhaps the best way is to pass a string array to the folder. However, this makes the call code a little more complicated, because an array needs to be constructed in advance and then passed as a parameter.

To simplify coding, C # provides a special keyword that allows you to provide variable numbers of parameters when calling a method, rather than the number of parameters that are fixed by the method in advance. Before discussing the declaration of this method, first observe the calling code in the main () method in the code list 4-14.

Code list 4-14 passing a variable-length parameter list
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. IO;

Namespace paramstest
{
Class Program
{
Static string combine (Params string [] paths)
{
String result = string. empty;
Foreach (string path in paths)
{
Result = system. Io. Path. Combine (result, PATH );
}
Return result;
}
Static void main (string [] ARGs)
{
String fullname;
//...
// Call combine () with four parameters
Fullname = combine (directory. getcurrentdirectory (), "bin", "Config", "index.html ");
Console. writeline (fullname );
//...
// Call combine () with only three parameters
Fullname = combine (environment. systemdirectory, "Temp", "index.html ");
Console. writeline (fullname );
//...
// Call combine () with an Array
Fullname = combine (New String [] {"C: \", "data", "homedir", "index.html "});
Console. writeline (fullname );
Console. readkey ();
//...
}
}
}
The output 4-8 shows the result of code listing 4-14.

Output 4-8

C:\Data\mark\bin\config\index.html
C:\WINDOWS\system32\Temp\index.html
C:\Data\HomeDir\index.html

In the first combine () call, we specify four parameters. The second call only specifies three parameters. In the last call, parameters are transmitted using an array. In other words, the combine () method accepts variable numbers of parameters, whether separated by commas or transmitted as an array as a whole.

To achieve this effect, the combine () method needs:

(1) Add a Params keyword before the last parameter of the method declaration;

(2) Declare the last parameter as an array.

After declaring a parameter array (parameter array), you can access each parameter as a member of the parameter array. In the implementation of the combine () method, we traverse every element of the paths array and call system. Io. Path. Combine (). This method can automatically merge all parts of a path and correctly use the platform-specific directory separator. Note that pathex. Combine () is equivalent to path. Combine (), but pathex. Combine () can process a variable number of parameters instead of only two.

Parameter arrays have the following notable features.

The parameter array is not necessarily the unique parameter in the method declaration. However, the parameter array must be the last parameter in the method declaration. Since only the last parameter can be a parameter array, a method can have only one parameter array.

The caller can specify zero parameters for the parameter array, resulting in an array containing zero data items.

The parameter array is type-safe. The type must match the specified type of the array.

The caller can explicitly provide an array instead of a comma-separated parameter list. The code generated is the same.

If the implementation of the target method requires a minimum number of parameters, explicitly specify required parameters in the method declaration. In this way, if the required parameter is missing, the compiler will be forced to report an error, rather than relying on runtime error handling. For example, use int max (INT first, Params int [] operads) instead of int max (Params int [] operads) to ensure that at least one value is passed to max ().

Using the parameter array, we can pass multiple parameters of the same type and quantity to a method. The Section 4.7 next to this chapter discusses how to support variable parameters of different types.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.