C # Variable Number Parameter params instance

Source: Internet
Author: User

In general, the number of parameters is fixed. The parameter defined as a cluster type can achieve a variable number of parameters,. NET provides a more flexible mechanism to implement variable number parameters, which is to use the params modifier. The benefit of a variable number parameter is that, in some cases, it is easy to implement an uncertain number of parameters. For example, you can calculate the weighted sum of any number and link any string to a string. Let's take a look at the example:

Copy codeThe Code is as follows:
Public class Test2
{
Public static void Main ()
{
ShowName ("Xiao Bing ");
ShowName ("John", "John ");
}
Public static void ShowName (params string [] names)
{
Foreach (string name in names)
{
Console. WriteLine (name );
}
}
}


Note:
1. The parameters modified by params must be a one-dimensional array.
2. The parameter array modified by params can be of any type, as long as the array type is set to object.
3. params must be at the end of the parameter list and can only be used once.

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.