Using the Params keyword in C #, the number of parameters in the method is variable.

Source: Internet
Author: User

I personally think that it is a major advantage of C # syntax to provide the Params keyword to Realize Variable Number of method parameters. In the method parameter list, the Params keyword is added before parameters of the array type. Generally, codes are more refined when calling methods.

For example, the following code:

[CSHARP]View plaincopy
  1. Class Program
  2. {
  3. Static void main (string [] ARGs)
  4. {
  5. Console. writeline (sum (1 ));
  6. Console. writeline (sum (1, 2, 3 ));
  7. Console. writeline (sum (1, 2, 3, 4, 5 ));
  8. Console. readkey ();
  9. }
  10. Private Static int sum (Params int [] values)
  11. {
  12. Int sum = 0;
  13. Foreach (INT value in values)
  14. Sum + = value;
  15. Return sum;
  16. }
  17. }

 

A sum method is implemented to receive a group of integers and return their sums. After the parameter values is added with the Params keyword, each element of this set of integers can be listed in the real parameter list during the call, which is very convenient.

Note the following when using the Params Keyword:

1. Params can only be used for one-dimensional arrays. It cannot be used for multi-dimensional arrays or any collection type similar to arrays, such as arraylist and list <t>.

2. The parameter with the Params keyword added must be the last parameter in the parameter list, and only one parameter is allowed in the method declaration.ParamsKeyword.

3. Methods Using the Params keyword can be called in four forms:

First, list the elements of the array: sum (, 3), which is also the most common form;

Second, use the array name as the real parameter: sum (New int [] {1, 2, 3}), like an array parameter without the Params keyword }) or int n = new int [] {1, 2, 3}; sum (N );;

Third, the parameter with the Params keyword can be omitted during the call: sum (); // return 0; in this way, sometimes one method overload can be defined, but when int sum () is explicitly defined, the compiler will first call int sum () instead of sum (Params int [] values ). If the Params parameter is omitted, an array with zero element count will still be added in the method, and the efficiency will be slightly checked.

Fourth, Params parameters are not omitted, and null is used instead. The efficiency is slightly higher than that of the third type, because the array is not new internally.

Using the Params keyword in C #, the number of parameters in the method is variable.

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.