c#允许方法带有不定数据的参数,实现改特性的语法使用params关键字。应用params关键字的参数必须是参数表中的最后一个参数
在方法声明中的 params 关键字之后不允许任何其他参数,并且在方法声明中只允许一个 params 关键字。
class
Program
{
static void Main( string [] args)
{
fct( " call 1 " );
fct( " call 2 " , 67 , 3 , " abcd " , 1.234 );
fct( " call 3 " , " hello " , 2.345 , 7 , " world " );
}
static void fct( string str , params object [] args)
{
Console.WriteLine(str);
foreach ( object obj in args)
{
if (obj is int )
Console.WriteLine( " int : " + obj);
else if (obj is double )
Console.WriteLine( " double: " + obj);
else if (obj is string )
Console.WriteLine( " string: " + obj);
else
Console.WriteLine( " other type: " + obj);
}
}
}
{
static void Main( string [] args)
{
fct( " call 1 " );
fct( " call 2 " , 67 , 3 , " abcd " , 1.234 );
fct( " call 3 " , " hello " , 2.345 , 7 , " world " );
}
static void fct( string str , params object [] args)
{
Console.WriteLine(str);
foreach ( object obj in args)
{
if (obj is int )
Console.WriteLine( " int : " + obj);
else if (obj is double )
Console.WriteLine( " double: " + obj);
else if (obj is string )
Console.WriteLine( " string: " + obj);
else
Console.WriteLine( " other type: " + obj);
}
}
}