Param array parameters

Source: Internet
Author: User

The param array allows us to write only one method to accept variable parameters. this technique is a parameter array, which is essentially a parameter declared using the params keyword. in addition, you can declare arrays such as params int [] list and parameter groups of the object type. The parameters can be of any type ~
Util class
# Region Using directives
Using System;
# Endregion
 
Namespace ParamsArray
{
Class Util
{
Public static int Sum (params int [] paramList)
{
If (paramList = null)
{
Throw new ArgumentException ("Util. Sum: null parameter list ");
}
 
If (paramList. Length = 0)
{
Throw new ArgumentException ("Util. Sum: empty parameter list ");
}
 
Int sumTotal = 0;
Foreach (int I in paramList)
{
SumTotal + = I;
}
Return sumTotal;
}
 
Public static void Everyone (params object [] paramobject)
{
If (paramobject = null)
{
Throw new Exception ("Util. Everyone: null parameter list ");
}
 
If (paramobject. Length = 0)
{
Throw new Exception ("Util. Everyone: empty parameter list ");
}
 
Foreach (object I in paramobject)
{
Console. WriteLine (I );
}
}
}
}
Program class
# Region Using directives
 
Using System;
Using System. Collections. Generic;
Using System. Text;
 
# Endregion
 
Namespace ParamsArray
{
Class Program
{
Static void Entrance ()
{
Console. WriteLine (Util. Sum (10, 9, 8, 7, 6, 5, 4, 3, 2, 1 ));
}
 
Static void Entrance01 ()
{
Console. WriteLine (Util. Sum (); // The length is 0.
}
 
Static void Entrance02 ()
{
Console. WriteLine (Util. Sum (null); // The array is null.
}
 
Static void Entrance03 ()
{
Util. Everyone ("dkjf", 45," 654 ", 451.5 );
}
 
Static void Main ()
{
Try
{
Entrance ();
Entrance01 ();
}
Catch (Exception ex)
{
Console. WriteLine ("Exception: {0}", ex. Message );
}
 
Try
{
Entrance02 ();
}
Catch (Exception ex)
{
Console. WriteLine ("Exception: {0}", ex. Message );
}
 
Try
{
Entrance03 ();
}
Catch (System. Exception ex)
{
Console. WriteLine ("Exception: {0}", ex. Message );
}
}
}
}
After each try statement is executed, the internal part of the try statement after the Exception will not be executed again, so another try statement should be written.
The statement execution result is as follows:

Author: "McDelfino"

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.