C # params passes multiple parameters

Source: Internet
Author: User

The params in the C # development language is a keyword that can specify method parameters that take parameters at variable number of arguments. The arguments in the function

It is useful to have a variable number of code and execute a very small difference!

The params keyword indicates that the parameter of the function is a variable number of method parameters, such as Console.WriteLine ("{0},{1}", i,j); It's like Delphi.

The Writeln function, which is used to represent the same type, but the number of arguments is indeterminate.

No additional arguments are allowed after the params keyword in the method declaration, and only one params keyword is allowed in the method declaration.

For an array of parameters, you need to know the following points.

(1) If a parameter array is included in the formal parameter list, then the parameter array must be at the end of the formal parameter lists;

(2) The parameter array must be a one-dimensional array;

(3) The params modifier is not allowed to be used in combination with the ref and out modifiers;

(4) An argument corresponding to a parameter array can be an array name of the same type, or it can be any number of variables of the same type as the elements of the array;

(5) If an argument is an array, it is passed by reference, and if the argument is a variable or an expression, it is passed by value.

(6) Usage: Variable method parameters, also known as array type parameters, suitable for the method parameter number of unknown cases, used to pass a large number of array set parameters; When using the

Array parameter, you can specify multiple method parameters in the formal parameter list by using the params keyword, and specify an array in the parameter table of the method, in the form: method modifier

return type method name (params type [] variable name)

{

Method body

}

The params uses code to demonstrate:

Class Program

{

static void Main (string[] args)

{

Sum ("a");

Console.readkey ();

}

static void Sum (params object[] numstack)

{

for (int i = 0; i < numstack.length; i++)

{

Console.WriteLine (Numstack[i]);

}

}

}

Output Result:

1

2

A

C # params passes multiple parameters

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.