C#中params关键字
每个C#函数都允许有个参数带params关键字,在调用的时候可以不给他传值,也可以给他传值,还可以给他传多个值,例子如下:
程序代码
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Test("Jack"));//不传值,显示Jack
Console.WriteLine(Test("Jack", "Hi"));//传一个值,显示Jack Hi
Console.WriteLine(Test("Jack", "Hi", "How are you?"));//传多个值,显示Jack Hi How are you?
Console.ReadKey();
}
static string Test(string name, params string[] args)
{
string msg = name;
for (int i = 0; i < args.Length; i++)
msg += " " + args[i];
return msg;
}
}
}
阅读(421) | 评论(0) | 转发(0) |