Reputation: 97
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);
}
}
Upvotes: 7
Views: 5228
Reputation: 1
Arrays
need to pass at least 1 argument to method to work.
params
can work without arguments in method.
No additional parameters are allowed after the params
keyword in a method declaration, and only one params
keyword is allowed in a method declaration.
static int test(params string[] test1)
{
return test1.Length;
}
Console.WriteLine($"\"params\" got {test("Japan", "Germany", "Maldives")} strings in the row."); // returns 3
Console.WriteLine($"\"params\" got {test()} strings in the row."); // returns 0
Upvotes: 0
Reputation: 11
Adding to the previous two answers. If we have a method
calculate(params int[] b)
Then it can be invoked with calculate(1,2,3)
Now if you would have used integer array calculate(int[] b)
. You won't be able to call the function like this calculate(1,2,3)
.
Upvotes: 1
Reputation: 679
In addition to Selman's answer, There are some obvious but minor limitations when using params
:
According to MS C# reference
No additional parameters are permitted after the params keyword in a method declaration, and only one params keyword is permitted in a method declaration.
This is because the compiler would have hard time identifying which parameter belongs to which.
The compiler will also complain if you have normal argument before the params
params
parameter must be the last in a formal parameter list.
For example this would not be allowed:
public void Foo(params int[] args,int value) { }
Upvotes: 2
Reputation: 101681
Using params
you can pass zero or more arguments, but with arrays, you have to prodive that argument if it's not optional.For example you can call this method without passing any argument and args will be empty:
public void Foo(params int[] args) { }
Foo(); // no compile-time error, args will be empty
But if you use an array:
public void Foo(int[] args) { }
Foo(); // error: No overload for 'Foo' takes 0 arguments
Otherwise there is not much difference between two. params
is just a syntactic sugar.
Upvotes: 13