Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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);      
}
}
like image 957
charles Avatar asked Dec 09 '14 09:12

charles


People also ask

What are params used for?

A parameter is a named variable passed into a function. Parameter variables are used to import arguments into functions. Note the difference between parameters and arguments: Function parameters are the names listed in the function's definition.

What is params array?

When you need an indefinite number of arguments, you can declare a parameter array, which allows a procedure to accept an array of values for a parameter. You do not have to know the number of elements in the parameter array when you define the procedure.

What is params params?

Specifically, params refers to the parameters being passed to the controller via a GET or POST request. In a GET request, params get passed to the controller from the URL in the user's browser.

What is params modifier?

This parameter modifier allows us to send in a variable number of arguments as a single logical parameter. A method can have only a single params modifier, and it must be the final parameter of the method. The default manner in which a parameter is sent into a function is by value.


2 Answers

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.

like image 152
Selman Genç Avatar answered Oct 13 '22 10:10

Selman Genç


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) { }
like image 25
RedRose Avatar answered Oct 13 '22 10:10

RedRose