C # required knowledge -- Variable Length Parameter -- Params,

Source: Internet
Author: User

C # required knowledge -- Variable Length Parameter -- Params,

When the number of parameters is variable, Params indicates that the number of parameters is unknown.

When using Params, you need to know the following:

1. If the parameters passed by the function contain multiple parameters, the parameter array marked with Params needs to be placed at the end

The figure is clearly displayed. You can only use the sort method of A without much explanation.

2. Params must be an array and must be a one-dimensional array.

3. Params cannot be combined with ref or out.

See the article http://www.cnblogs.com/hunts/archive/2007/01/13/619620.html of Hunts. C predecessors

4. The real parameter corresponding to the parameter array modified by Params can be an array name of the same type (Note: Only one array name is allowed, and multiple array names are not allowed ), it can also be any number of variables of the same type as the elements of the array.

DEMO code

Class Program {static void Main (string [] args) {// The display parameter is variable int I = Sum (1, 13, 23, 34); Console. writeLine (I); int j = Sum (1, 1, 3, 2, 4, 4, 44,555, 6); Console. writeLine (j); // The real parameter can be an array name int [] ArrayI = new int [5] {1, 2, 3, 4, 5 }; int ArraySum = Sum (ArrayI); Console. writeLine (ArraySum); Console. read ();} static int Sum (params int [] s) {int sum = 0; foreach (int I in s) {sum + = I ;}return sum ;}}

 

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.