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
The documentation for optional arguments says:
Since
new string[0]is neither a constant expression nor anewstatement 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: