30

Possible Duplicate:
passing an empty array as default value of optional parameter in c#

I have a method that looks like below. Currently, the parameter tags is NOT optional

void MyMethod(string[] tags=null)
{
   tags= tags ?? new string[0];
   /*More codes*/
}

I want to make parameter tags optional, as per c# , to make a parameter optional you can set a default value in method signature. I tried the following hacks but none worked.

Code that didn't work - 1

void MyMethod(string[] tags=new string[0]){}

Code that didn't work - 2

void MyMethod(string[] tags={}){}

Please suggest what I am missing.


I have already seen this question:

Passing an empty array as default value of an optional parameter

Shark Lasers
  • 441
  • 6
  • 15
Rusi Nova
  • 2,617
  • 6
  • 32
  • 48
  • Take a look here: http://stackoverflow.com/questions/3480382/passing-an-empty-array-as-default-value-of-optional-parameter-in-c-sharp – nowaq Nov 21 '11 at 14:58
  • 2
    in this particular case, you might get away with C#3.0 compatible `void MyMethod(params string[] tags)` – sehe Nov 21 '11 at 14:58
  • 1
    What's wrong with `tags = tags ?? new string[0];` anyway? And if you have already seen the question, the accepted answer clearly states that you *can't* set a default value with the C# 4 syntax, and that you can use the `??` operator instead as you are doing. – BoltClock Nov 21 '11 at 14:58
  • `tags` looks optional enough to me in your original method. – Anthony Pegram Nov 21 '11 at 15:00
  • What's wrong with `string[] tags=null` exactly? It does work. – H H Nov 21 '11 at 15:01
  • @boltclock with `tags = tags ?? new string[0];` tags is not optional. – Rusi Nova Nov 21 '11 at 15:01
  • 2
    @Rusi Nova: It is optional if you set a default value of `null`. – BoltClock Nov 21 '11 at 15:02
  • 4
    @RusiNova, we might be using a different definition of optional. To be clear, as far as the language is concerned, `void Foo(string[] tags = null)` indeed makes supplying the argument *optional* for the caller. Whether or not the null is *usable* inside your method is a different matter. – Anthony Pegram Nov 21 '11 at 15:03

2 Answers2

49

The documentation for optional arguments says:

A default value must be one of the following types of expressions:

  • a constant expression;

  • an expression of the form new ValType(), where ValType is a value type, such as an enum or a struct;

  • an expression of the form default(ValType), where ValType is a value type.

Since new string[0] is neither a constant expression nor a new statement followed by a value type, it cannot be used as a default argument value.

The first code excerpt in your question is indeed a good workaround:

void MyMethod(string[] tags = null)
{
   tags = tags ?? new string[0];
   // Now do something with 'tags'...
}
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • +1 And the workaround is suggested in the accepted answer to the duplicate question. – BoltClock Nov 21 '11 at 15:00
  • 1
    This is correct but if you use MyMethod(params Object[] parameterName) You can omit the params array and the compiler will pass an empty array for you. MyMethod() works just fine. – jwize Feb 15 '14 at 09:08
  • Upvoted for linking and in-lining the relevant authoritative documentation. "Teach a man to fish..." – aaronsnoswell Oct 19 '17 at 23:44
5

not sure if i am getting it right this works.

  static void Main(string[] args)
        {
                TestMe();

        }


private static void TestMe(string[] param = null)
    {
        if (param == null)
        { 
            Console.WriteLine("its empty");
        }
    }

also the value of param has to be compile time constant

np-hard
  • 5,725
  • 6
  • 52
  • 76