What is the difference between "params" and "array parameter" and when should it be used?
Asked Answered
W

4

7

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);      
}
}
Wyandotte answered 9/12, 2014 at 9:41 Comment(0)
A
13

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.

Aeriell answered 9/12, 2014 at 9:45 Comment(1)
to get no error with parameter use it like:: public void Foo(int[] args = null) { }Flense
I
2

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) { }
Interfaith answered 9/5, 2019 at 18:31 Comment(0)
U
1

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).

Unbreathed answered 22/6, 2021 at 11:56 Comment(1)
Oh so you mean if I use an integer array, I have to pass an array as a parameter right?Despumate
V
0
  1. Arrays need to pass at least 1 argument to method to work.

  2. params can work without arguments in method.

  3. 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;
     }
    
  • Calling the method with some arguments.

Console.WriteLine($"\"params\" got {test("Japan", "Germany", "Maldives")} strings in the row."); // returns 3

  • Calling the method with no argument. Arrays can't do.

Console.WriteLine($"\"params\" got {test()} strings in the row."); // returns 0

Valentia answered 19/4, 2023 at 17:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.