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
void MyMethod(params string[] tags)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.null.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.