C # variable Number parameter params instance _ practical Tips

Source: Internet
Author: User
Generally, the number of parameters is fixed, and the parameters defined as cluster types can achieve the purpose of variable number of parameters, but. NET provides a more flexible mechanism for implementing variable-number parameters, which is the use of the params modifier. The advantage of a variable number of parameters is that in some cases it is convenient to implement an indeterminate number of parameters, such as calculating the weighting of any number and linking any string to a string. Look at the example:

Copy Code code as follows:

public class Test2
{
public static void Main ()
{
ShowName ("Pawn");
ShowName ("Xiao Wang", "Xiao Liu");
}
public static void ShowName (params string[] names)
{
foreach (string name in Names)
{
Console.WriteLine (name);
}
}
}


Attention:
1, the params decorated parameters must be one-dimensional array.
2, params decorated parameter array, can be any type, as long as the array type is set to object.
3, params must be in the last one of the parameter list, and can only be used once.

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.