C # functions and usage of param

Source: Internet
Author: User

If a large number of parameters are required in a method, you can use Param to integrate these parameters into an array.

For example:

 
Int add (int A, int B, int C, int D ,......)
 
{
 
Return A + B + C + D + ......;
 
}
 
 
 
You will find it better to change the parameter to an array.
Int add (INT [] ARGs)
 
{
 
Int sum = 0;
 
Foreach (INT number in ARGs)
 
{
 
Sum + = number;
 
}
 
Return sum;
}
 
 
 
But it is inconvenient to use. If you want to use add (1, 2), you have to put 1, 2 in the array.
 
Int [] array = new int {1, 2 };
 
In this case, you can add Params to the above method.
 
Int add (Params int [] ARGs ){......}
 
Then, you only need to add (1, 2), add (7, 8, 9, 10, 12 )......
 
 

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.