Friday, June 02, 2006

Params Keyword

The params keyword lets you specify a method parameter that takes an argument where the number of arguments is variable.

For example,

static void Main()
{
TestArgsMethod("Hello", " goodbye ", " hello");
TestArgsMethod("Numbers", 1, 2, 3, 4);
}

static void TestArgsMethod(String text, params Object[] args)
{
StringBuilder sb = new StringBuilder();
sb.Append(text);
foreach(Object o in args)
{
sb.Append(o.ToString());
}
Debug.WriteLine(sb.ToString());
}

No comments: