charles
charles

Reputation: 97

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

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

Answers (4)

Hello_CSharp
Hello_CSharp

Reputation: 1

  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

Upvotes: 0

RG1nine
RG1nine

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

RedRose
RedRose

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

Selman Gen&#231;
Selman Gen&#231;

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

Related Questions