class OptionalParameters { [STAThread] static void Main(string[] args) { //call optionalStrings with no arguments optionalStrings(); //call with 5 arguments optionalStrings("One","Two","Three","Four", "Five"); //pause here so we can see the output Console.ReadLine(); } public static void optionalStrings(params String[] args) { //args is an actual array, so we can use any array methods Console.WriteLine("Number of arguments: " + args.GetLength(0).ToString()); for (int i = 1; i<args.GetLength(0)+1; i++) { Console.WriteLine("Argument #{0} {1}",i.ToString(),args[i-1]); } } } |