params 使普通的函数数组参数 int[] a,变成了一个可变变量类型,与Java中的int... a类似,以下代码一目了然:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Chap01 7 { 8 class Program 9 { 10 static void printArray( int [] a) 11 { 12 foreach ( int i in a) 13 { 14 Console.WriteLine(i); 15 } 16 } 17 18 static void printVars( params int [] a) 19 { 20 foreach ( int i in a) 21 { 22 Console.WriteLine(i); 23 } 24 } 25 static void Main( string [] args) 26 { 27 int [] iArray = { 1 , 2 , 3 , 4 , 5 }; 28 printArray(iArray); 29 printVars( 1 , 2 , 3 , 4 , 5 , 6 , 7 ); 30 } 31 } 32 }打印结果:
1 1 2 2 3 3 4 4 5 5 6 1 7 2 8 3 9 4 10 5 11 6 12 7 13 请按任意键继续. . .
转载于:https://www.cnblogs.com/nysanier/archive/2011/05/23/2054727.html
