C # defines a one-dimensional parameter array with the params

Source: Internet
Author: User
Tags modifiers

Parameter array, which defines the parameter array through the keyword params, the params type parameter is mainly used for function declaration in the case of unknown (variable) length of the array, which can be passed with different number of arguments, and has good flexibility.


First, give a concrete example of the specific use of the parameter array (params):

Example ①:

static void Main (string[] args) {T (' s ', ' a ', ' g ', ' e ', ' w ', ' Q ');            Error: T method does not take 6 parameters of overload T (new object[] {' A ', ' G ', 1, "Little Dragon Girl"});    public static void T (object[] c) {for (int i = 0; i < c.length; i++) {Console.Write (C[i] + ""); }}

Example ②:

static void Main (string[] args) {T (' s ', ' a ', ' g ', ' e ', ' w ', ' Q ');            Error: T method does not take 6 parameters of overload T (new object[] {' A ', ' G ', 1, "Little Dragon Girl"});    public static void T (params object[] c) {for (int i = 0; i < c.length; i++) {Console.Write (C[i] + ""); }}

Example ①, example ② comparison will be found that the parameter array without the params can only be passed in an array, after adding the params, you can pass in the array or into multiple parameters.


The following summarizes some 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.


Example ③:

Class program{public static void min (int[] list) {} public static void min (params int[] list)//compile time Error message: Type program has defined a member with the same parameter type named min {}}

Example ③ verified the 2.



3. The ref or out params array is not allowed.


Example ④:

public static void Min (out params int[] list)//compile-time error prompt: There are too many modifiers on this parameter {} public static void Max (ref param s int[] list)//compile-time error prompt: Too many modifiers on this parameter {}


4. the params array must be the last parameter of the method (that is, there can be only one params array parameter).


Example ⑤:

public static void Min (params int[] a,int["b,int[] C)//error Hint: The params parameter must be the last parameter in the formal parameters table {} public static void Ma X (int[] a,int[] b,params int[] C) {}


5. the compiler checks for and rejects 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.


Example ⑥:

public static void min (Int. a,int B) {} public static void min (params int[] C) {}

The first to be called (declaring a method without params array parameters, perhaps a useful optimization technique, avoids the compiler creating and populating too many arrays).


7. Black.hole (black hole), params object[] list.


Example ⑦:

public static void Hole (params object[] C) {}


System.Object (Object) is the root of all classes, using the params Object array as a parameter in the formal parameter list to declare a method that can accept any number of Object arguments, in other words, not only the number of parameters is arbitrary, the type of the parameter can be arbitrary. So, this method is called black.hole (black Hole)


①. You can not pass any parameters to it;


②. You can pass NULL as an argument when it is called;


③. You can pass an actual array to it. In other words, an array created by the compiler can be created manually;


④. You can pass any other parameter of different types to it, and these parameters will be automatically encapsulated into an object array.

C # defines a one-dimensional parameter array with the 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.