-1

I have something super similar to:

private void Method(string[] Parameters= {})
{
    // execute code here
}

The issue I run into here is an error stating:

invalid expression term '{'

so if I remove the '= {}' I no longer have an optional string[] as a parameter. I need to have a string[] as an optional parameter.

Let me know if I need to clarify anything.

karel
  • 5,489
  • 46
  • 45
  • 50
  • see this https://stackoverflow.com/questions/3480382/passing-an-empty-array-as-default-value-of-an-optional-parameter – apomene Feb 13 '19 at 15:50
  • @whoever closed my question, that one you marked as duplicate was before 4.0 was released. Im certain there is an implementation of something better in 4.0. – LOGANr18 Feb 13 '19 at 16:05
  • Dear LOGAN. I was the one that initially tried to answer your question and after better search marked it as duplicate. From my narrow knowledge I believe this holds for new versions of .NET. In any case if you are certain there exists a better implementation why not try to search before asking? – apomene Feb 13 '19 at 16:12
  • @LOGANr18 If you, or anyone else, comes up with a better solution to the problem than what's already on the existing question, you/they are free to post a new answer with a different solution. – Servy Feb 13 '19 at 16:13

1 Answers1

1

You need to supply null as the default value for an optional string array.

  private void Method(string[] Parameters = null)
  {
        if (Parameters == null)
        {
            // optional parameter not passed in
        }
        else
        {
            // do work with the parameter
        }
  }
Murray Foxcroft
  • 12,785
  • 7
  • 58
  • 86