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"