C # difficulties 1 by 1 (3): Params array parameters

Source: Internet
Author: User

Only one paras keyword is allowed in the method declaration, and this keyword can only be the last one. CopyCode The Code is as follows: using system;

/******************************
* Chapter: C # difficulties one by one (3)
* Author: Wang Hongjian
* Date: 2010-1-16
* blog: http://www.51obj.cn/
* Email: walkingp@126.com
* description: use of array parameter Params
****************************/
namespace testparams
{< br> class Program
{< br> Public static class paramsclass
{< br> ///


// /Two parameters, the last parameter is the array parameter
///
///
///
Public static void paramsmethod (INT num, params string [] ARGs)
{< br> foreach (string _ ARGs in ARGs)
{< br> console. writeline (_ ARGs);
}< br> console. writeline ("total num is" + num);
console. readkey ();
}< BR >}< br> static void main (string [] ARGs)
{< br> string [] strarr = {"Wang Hongjian", "Chenchen", "dodo" };< br> int personnum = strarr. length;
paramsclass. paramsmethod (personnum, strarr);
}< BR >}

Running effect:

To sum up ref, out, And Params, ref is the result of the parameter impact in the reference method; out is the result returned by the parameter in the reference method to the main method; and Params is the variable similar parameter (that is, the array) input parameters. In use, ref is used to call the data in the reference method. The out clause is used to operate the main method using the reference method, while the prarams clause is used for the case where the parameter length is unknown.

Source code download

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.