Example of variable number of parameters implemented by the params keyword in C #

Source: Internet
Author: User

Personally, it is a great advantage of C # syntax to provide the params keyword to achieve a variable number of method parameters. In the method parameter list, the parameters of the array type are pre-added with the params keyword, which can often be more refined when calling the method.

For example, the following code:

Class program{static void Main (string[] args) {Console.WriteLine (Sum (1)); Console.WriteLine (Sum (1, 2, 3)); Console.WriteLine (Sum (1, 2, 3, 4, 5)); Console.readkey ();} private static int sum (params int[] values) {int sum = 0;foreach (int value in values) sum + = Value;return sum;}}

Implements a sum method that receives a set of integers and returns their sum. After the parameter values are added to the params keyword, it is convenient to enumerate each element of the set of integers in the argument list at the time of invocation.

For the use of the params keyword, the following points need to be noted:

1. The params can only be used for one-dimensional arrays, not for multidimensional arrays and any array-like collection types such as ArrayList, List<t>, and so on.

2. The parameter that is added to the params keyword must be the last parameter in the formal parameter list, and only one params keyword is allowed in the method declaration.

3. With the params keyword method, there are four kinds of invocation forms:

The first is to enumerate the elements of the array: Sum (three to three), which is also the most commonly used form;

The second, like an array parameter without the params keyword, does the argument with the array name: Sum (New int[]{1,2,3}) or int n=new int[]{1,2,3}; Sum (n);;

Third, the parameters of the params keyword can be omitted at the time of Invocation: Sum ();//return 0; This way sometimes you can define a method overload less, but when the overloaded int sum () is explicitly defined, the compiler will call int sum () instead of sum (params int[] Values). and omit the params parameter, the method will still be a new array of the number of elements 0, the efficiency is slightly checked.

The fourth type, do not omit the params parameter, with NULL instead, the efficiency is slightly higher than the third, because its interior will not be new this array.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
Example of variable number of parameters implemented by the params keyword in C #

This address: http://www.paobuke.com/develop/c-develop/pbk23465.html






Related content C # video or sound player source code created using MCI download C # Two simple code examples for connecting to MySQL C #???? ̨ó|ó?3ìdò? Dê?3?2êé?x?ì? Timer usage in C # and resolving re-entry issues
Regular expression instances commonly used in C # algorithm functions: Gets the maximum length in a string C # method of encrypting large files with DES encryption algorithm C # Multithreading Learning (v) automatic management of multithreading using timers

Example of variable number of parameters implemented by the params keyword in C #

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.