-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.

4
  • see this stackoverflow.com/questions/3480382/… Commented Feb 13, 2019 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. Commented Feb 13, 2019 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? Commented Feb 13, 2019 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. Commented Feb 13, 2019 at 16:13

1 Answer 1

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 } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.