Passing an empty array as default value of an optional parameter

84,564

Solution 1

You can't create compile-time constants of object references.

The only valid compile-time constant you can use is null, so change your code to this:

public void DoSomething(int index, ushort[] array = null,
  bool thirdParam = true)

And inside your method do this:

array = array ?? new ushort[0];

(from comments) From C# 8 onwards you can also use the shorter syntax:

array ??= new ushort[0];

Solution 2

If you can make the array the last argument you could also do this:

public void DoSomething(int index, bool wasThirdParam = true, params ushort[] array)

The compiler will automatically pass an empty array if it is not specified, and you get the added flexibility to either pass an array as a single argument or put the elements directly as variable length arguments to your method.

Solution 3

I know it's an old question, and whilst this answer doesn't directly solve how to get around the limitations imposed by the compiler, method overloading is an alternative:

   public void DoSomething(int index, bool thirdParam = true){
        DoSomething(index, new ushort[] {}, thirdParam);
   }

   public void DoSomething(int index, ushort[] array, bool thirdParam = true){

      ...
   }
Share:
84,564

Related videos on Youtube

MisterG13
Author by

MisterG13

Updated on April 01, 2020

Comments

  • MisterG13
    MisterG13 about 4 years

    How does one define a function that takes an optional array with an empty array as default?

    public void DoSomething(int index, ushort[] array = new ushort[] {},
     bool thirdParam = true)
    

    results in:

    Default parameter value for 'array' must be a compile-time constant.

    • Admin
      Admin over 6 years
      This is not a duplicate. It is specific in asking how to set a default value for an array, not a string. There is a real difference in approaches
  • MisterG13
    MisterG13 over 13 years
    yes, that does come to mind, except passing null is not the same as an empty array. I was hoping for a way to do it without having to dress the param inside the method. The bit about ompile-time constants of object references makes sense. Thanks!
  • Ayyash
    Ayyash almost 9 years
    that throws ArgumentNullException, anybody knows why?
  • Lasse V. Karlsen
    Lasse V. Karlsen almost 9 years
    Yes, you're not fixing the case that you are actually receiving null, did you use the second statement from my answer?
  • Hi-Angel
    Hi-Angel almost 9 years
    I am not sure whether a compiler could then infer and optimize a mutual recursion instead of an usual (though yes, I know e.g. there is an option -foptimize-sibling-calls in g++, but it is C++, I am not sure whether C# compilers can it).
  • Éric Bergeron
    Éric Bergeron over 4 years
    I just wanted to mention that C# 8 now allows a prettier syntax. array ??= new ushort[0];
  • Kuroro
    Kuroro about 4 years
    Null-coalescing assignment operator ??= is available in C# 8
  • Yousef
    Yousef over 3 years
    Probably you can consider Array.Empty<ushort>()
  • Thomas
    Thomas over 3 years
    And now tweak that for records ;) ... it is annoying ;)