Parameter array.
Parameter arrays allows us to pass a variant number of arguments into a method. All incoming arguments will consider as a single dimensional array. Have a look on the following example.
using System;
class Mainclass
{
public static void Main()
{
Test obj = new Test();
obj.Add(10, 20);
obj.Add(10, 20, 30);
obj.Add(1, 2, 3, 4, 5, 6);
}
}
class Test
{
public void Add(params int[] ar)
{
int sum = 0;
foreach (int i in ar)
{
sum = sum + i;
}
Console.WriteLine("Sum : " + sum);
}
}
No comments:
Post a Comment