What is the use of params keyword? Give an example.

Tuesday, July 14, 2009


params is used on such situations where the number of arguments to a method is not known until runtime. If you prefix the params keyword followed by the type as array without size, then you can pass as many arguments as you want.


Ex:
using System;
using System.Collections.Generic;
using System.Text;

namespace ParamSample
{
class Program
{
public static void PrintWords(params String[] p)
{
for (int i = 0; i < p.GetLength(0); i++)
{
System.Console.WriteLine(p[i]);
System.Console.Read();
}
}

static void Main(string[] args)
{
PrintWords("1","2","3");
}
}
}



www.codecollege.NET

0 comments

Post a Comment

www.codecollege.NET

Blog Widget by LinkWithin