What is the exact difference between these two methods? When to use "params" and when to use array parameters? A reply would be greatly appreciated.
// passing array to a method
class Program
{
static void printarray(int[] newarray)
{
int i,sum=0;
Console.Write("\n\nYou entered:\t");
for (i = 0; i < 4; i++)
{
Console.Write("{0}\t", newarray[i]);
sum = sum + newarray[i];
}
Console.Write("\n\nAnd sum of all value is:\t{0}", sum);
Console.ReadLine();
}
static void Main(string[] args)
{
int[] arr = new int[4];
int i;
for (i = 0; i < 4; i++)
{
Console.Write("Enter number:\t");
arr[i] = Convert.ToInt32(Console.ReadLine());
}
// passing array as argument
Program.printarray(arr);
}
}
}
//using parameter arrays
public class MyClass
{
public static void UseParams(params int[] list)
{
for (int i = 0; i < list.Length; i++)
{
Console.Write(list[i] + " ");
}
Console.WriteLine();
}
static void Main()
{
UseParams(1, 2, 3, 4);
int[] myIntArray = { 5, 6, 7, 8, 9 };
UseParams(myIntArray);
}
}