C # need to know-length variable parameter--params

Source: Internet
Author: User

The params is used when the number of parameters is variable, that is, the number of parameters is unknown.

You need to know the following points when using the params:

1. If the function passes a parameter containing more than one, the parameter array using the params tag needs to be placed in the last

The diagram shows clearly that there is no need to explain and only use the sort of a.

2, the params modified must be an array, but also a one-dimensional array

3. Params cannot be combined with ref, out

See HUNTS.C predecessor's article for details http://www.cnblogs.com/hunts/archive/2007/01/13/619620.html

4. The argument corresponding to the params modified parameter array can be an array name of the same type ( Note: Only one array name, multiple array names are not allowed ), or any number of variables of the same type as the elements of the array

Demo Code

classProgram {Static voidMain (string[] args) {            //The display parameters are variable            inti = Sum (1, -, at, the);            Console.WriteLine (i); intj = Sum (1,1,3,2,4,4, -,555,6);            Console.WriteLine (j); //an argument can be an array name            int[] Arrayi =New int[5] {1,2,3,4,5 }; intArraysum =Sum (Arrayi);            Console.WriteLine (Arraysum);        Console.read (); }        Static intSum (params int[] s) {intsum =0; foreach(intIinchs) {sum+=i; }            returnsum; }    }

C # need to know-length variable parameter--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.