C# - Is it possible to have null params?

C#ParametersParams

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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

string[] array = null;
Foo(array);

Solution 2 - C#

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.

Solution 3 - C#

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.

Solution 4 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmichaelView Question on Stackoverflow
Solution 1 - C#Jon SkeetView Answer on Stackoverflow
Solution 2 - C#Joshua RodgersView Answer on Stackoverflow
Solution 3 - C#codymanixView Answer on Stackoverflow
Solution 4 - C#Adriano CarneiroView Answer on Stackoverflow