Use of the parameter array (params)

Source: Internet
Author: User

Considerations for using parameter arrays:
1. Use the params keyword only on one-dimensional arrays.
2. You cannot overload a method that is based on the params keyword only. The params keyword does not form part of the method's signature.
Such as:
Compile-time error: Duplicate access
public static int Min (int [] paramlist)
.............
public static int Min (params int [] paramlist)
.............
3. The ref or out params array is not allowed
Compile-time error
public static int Min (ref params int [] paramlist)
.............
public static int Min (out params int [] paramlist)
.............
4. The params array must be the last parameter of the method (i.e. there can be only one params array parameter)
public static int Min (params int [] paramlist,int i)
.............
5. The compiler will check and reject any overloads that may be ambiguous
6. The non-params method always takes precedence over a params method. In other words, if you prefer, you can still create an overloaded version of a method for normal situations.

such as: Public Static intMin (intLefthandside,intrighthandside) better than: Public Static intMin (params int[] paramlist)usingSystem; Public classMyClass { Public Static voidUseparams (params int[] list) {             for(inti =0; I < List. Length; i++) {Console.WriteLine (list[i]);             } Console.WriteLine (); }          Public Static voidUSEPARAMS2 (params Object[] list) {              for(inti =0; I < List. Length; i++) {Console.WriteLine (list[i]);            } Console.WriteLine (); }            Static voidMain () {Useparams (1,2,3); USEPARAMS2 (1, the the; a&# the;, "test"); An array of objects can also is passed, as Long  asThe array type matches the method being called. int[] MyArray =New int[3] {Ten, One, A };            Useparams (myarray); }} The output of the program is as follows:1231atestTen One A

Use of the parameter array (params)

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.