Pass a variable number of parameters to the method.

Source: Internet
Author: User

Pass a variable number of parameters to the method.

Public sealed class program {public static void Main () {DisplayTypes (new object (), new double [] {}, "aaa", 5 );} private static void DisplayTypes (params Object [] objects) {if (objects! = Null) {foreach (Object o in objects) Console. WriteLine (o. GetType () ;}console. ReadKey ();}}
Output result // System. Object // System. Double [] // System. String // System. Int32

About params

C # In development language, params is a keyword. You can specify the parameter method parameter when the number of parameters is variable. It is useful when the number of function parameters is variable and the difference in code executed is very small! After the params keyword in the method declaration, no other parameters are allowed, and only one params keyword is allowed in the method declaration. You need to know the following about parameter Arrays. (1) If a parameter array is included in the parameter table, the parameter array must be at the end of the parameter list; (2) the parameter array must be a one-dimensional array; (3) it is not allowed to combine the params Modifier with the ref and out modifiers. (4) The real parameters corresponding to the parameter array can be array names of the same type, it can also be any number of variables of the same type as the elements of the array. (5) if the real parameter is an array, it is passed by reference. If the real parameter is a variable or expression, it is passed by value. (6) usage: variable method parameters, also called array parameters, are suitable for passing a large number of array set parameters when the number of method parameters is unknown, you can use the params keyword to specify multiple method parameters in the form: the method modifier returns the type method name (params type [] variable name), which is an extension of a one-dimensional array.

Related Article

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.