72
public void Foo(params string[] values)
{
}

Is it possible that values can ever be null, or will it always be set with 0 or more items?

Pooven
  • 1,744
  • 1
  • 25
  • 44
michael
  • 14,844
  • 28
  • 89
  • 177
  • 2
    Well, I don't know how you can pass in null to that function and have it assign to values. I guess I'm asking "is it possible to do that?" – michael Jul 05 '11 at 14:17
  • 4
    `Foo(null)` might be worth a try. – John Saunders Jul 05 '11 at 14:18
  • 16
    "Try it out" hardly ever gives a definitive answer. A `params` method can be called in several ways, making this a valid question. – H H Jul 05 '11 at 14:43
  • 2
    Suppose you also have an additional overload "public void Foo(object value){}" and you call "Foo(null)" -- what do you think happens now, and why? Try it -- were you right? – Eric Lippert Jul 05 '11 at 15:08
  • 1
    @JohnSaunders Foo(null) in this case will produce values = string[]{ null }, Foo((string[])null), WILL produce values = null, though. – Andrew Theken Jul 26 '13 at 14:45

4 Answers4

84

Absolutely - you can call it with an argument of type string[] with a value of null:

string[] array = null;
Foo(array);
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 2
    @Schroedingers, you are incorrect. Passing null in place of a params array will create a null reference array. A zero length array would be created only if you explicitly pass one in *or* omit arguments in the method call entirely. An array of a single null element would have to be created explicitly. – Anthony Pegram Jul 05 '11 at 14:21
  • @Schroedingers, Jon and Anthony are right. See the [demo](http://ideone.com/xhpdI). If you pass an array reference (which includes the regular `null` literal), `values` will be exactly that. On the other hand, if you pass a *string* reference that happens to be null, it will be an array of length 1 like you said. – Matthew Flaschen Jul 05 '11 at 14:24
  • 1
    @Jon: I was under the (strong) impression that the OP was asking about what happened in the case of `Foo()`. You can check my reasoning in my now-deleted answer in case you're interested :) – sehe Jul 05 '11 at 14:43
  • 4
    @sehe: There's nothing in the question to indicate that. The OP only asked if it's possible for `values` to be null - not what would happen if you called it without any arguments. – Jon Skeet Jul 05 '11 at 14:44
  • 1
    @Jon: Thanks for always being helpful when most everyone else were being dismissive. Clearly the OP (as well as myself) was asking for non-obvious cases where it could be `null`. Until today, I've always checked `params` arguments to see if they were `null` before using them, but never confirmed that it was actually possible for the value to be `null`. Thanks for the definitive answer. – David Schwartz Sep 03 '13 at 17:47
  • How can I avoid that with `enable`? – Robert S. Nov 18 '21 at 09:29
  • @RobertS.: I don't know what you mean by "that" in this case - but I'd suggest that a new question would be more suitable than requesting additional information on a question that's over 10 years old. – Jon Skeet Nov 18 '21 at 10:15
  • Ok will do. Thanks – Robert S. Nov 18 '21 at 10:23
55

I decided to write some code up to test this for myself. Using the following program:

using System;

namespace TestParams
{
    class Program
    {
        static void TestParamsStrings(params string[] strings)
        {
            if(strings == null)
            {
                Console.WriteLine("strings is null.");
            }
            else
            {
                Console.WriteLine("strings is not null.");
            }
        }

        static void TestParamsInts(params int[] ints)
        {
            if (ints == null)
            {
                Console.WriteLine("ints is null.");
            }
            else
            {
                Console.WriteLine("ints is not null.");
            } 
        }

        static void Main(string[] args)
        {
            string[] stringArray = null;

            TestParamsStrings(stringArray);
            TestParamsStrings();
            TestParamsStrings(null);
            TestParamsStrings(null, null);

            Console.WriteLine("-------");

            int[] intArray = null;

            TestParamsInts(intArray);
            TestParamsInts();
            TestParamsInts(null);
            //TestParamsInts(null, null); -- Does not compile.
        }
    }
}

The following results are yielded:

strings is null.
strings is not null.
strings is null.
strings is not null.
-------
ints is null.
ints is not null.
ints is null.

So yes, it is entirely possible for the array associated with params to be null.

Joshua Rodgers
  • 5,317
  • 2
  • 31
  • 29
5

My first guess was to declare the parameter with default value of null, which would make sense in some cases, but the c# language does not allow this.

static void Test(params object[] values = null) // does not compile
{
}

error CS1751: Cannot specify a default value for a parameter array

The way of getting around this limitation by explicitly passing null was already answered.

codymanix
  • 28,510
  • 21
  • 92
  • 151
3

In addition to Jon's answer, you can also have something like this:

string[] array1 = new string[]; //array is not null, but empty
Foo(array1);
string[] array2 = new string[] {null, null}; //array has two items: 2 null strings
Foo(array2);
Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123