标签:
每个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; } } }
·一个函数中只能一个参数带params关键字;
·带params关键字的参数必须是最后一个参数;
·带params关键字的参数类型必须是一维数组;
标签:
原文地址:http://www.cnblogs.com/nele/p/4964022.html