Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing zero arguments as params -- where the behaviour is defined?

C# spec. allows you to call a function

void foo(params int[] x)

with zero parameters. However, I didn't find in C# Lang. Spec. a word on further behaviour -- will foo get empty array or null reference? I checked also MSDN -- nothing.

Where the behaviour is defined?

NOTE: I am not asking how VS behaves, I am asking about design of the language.

like image 789
greenoldman Avatar asked May 31 '10 07:05

greenoldman


People also ask

What is params method?

By using the params keyword, you can specify a method parameter that takes a variable number of arguments. The parameter type must be a single-dimensional array. No additional parameters are permitted after the params keyword in a method declaration, and only one params keyword is permitted in a method declaration.

Do you pass arguments or parameters?

Arguments are passed by value; that is, when a function is called, the parameter receives a copy of the argument's value, not its address.

What is params argument in C#?

In C#, params is a keyword which is used to specify a parameter that takes variable number of arguments. It is useful when we don't know the number of arguments prior. Only one params keyword is allowed and no additional parameter is permitted after params keyword in a function declaration.

What happens if I call a JS method with less parameters than it is defined to accept?

When a function is invoked with fewer arguments than are declared, the additional arguments have the undefined value.


2 Answers

Section 7.4.1 of the C# Language Specification (ref: C# 3.0 spec)

In particular, note that an empty array is created when there are zero arguments given for the parameter array.

It's the last line of the section

like image 73
Anthony Pegram Avatar answered Oct 11 '22 14:10

Anthony Pegram


17.5.1.4 Parameter arrays

A parameter array permits arguments to be specified in one of two ways in a method invocation:

• The argument given for a parameter array can be a single expression of a type that is implicitly convertible (§13.1) to the parameter array type. In this case, the parameter array acts precisely like a value parameter.

• Alternatively, the invocation can specify zero or more arguments for the parameter array, where each argument is an expression of a type that is implicitly convertible (§13.1) to the element type of the parameter array. In this case, the invocation creates an instance of the parameter array type with a length corresponding to the number of arguments, initializes the elements of the array instance with the given argument values, and uses the newly created array instance as the actual argument.

In the same section an example is given:

using System;
class Test
{
    static void F(params int[] args) {
        Console.Write("Array contains {0} elements:", args.Length);
        foreach (int i in args)
            Console.Write(" {0}", i);
        Console.WriteLine();
    }

    static void Main() {
        int[] arr = {1, 2, 3};
        F(arr);
        F(10, 20, 30, 40);
        F();
    }
}

produces the output

Array contains 3 elements: 1 2 3 Array
contains 4 elements: 10 20 30 40 Array
contains 0 elements:

This example illustrates the expected behavior: empty array

like image 43
Darin Dimitrov Avatar answered Oct 11 '22 14:10

Darin Dimitrov