How can I set default parameter value for an action when that parameter is dictionary type?
for example:
public void Test(Dictionary<int, bool> dic) {
...
}
You can't give it a default value the way you want to because it has to be a compile time constant value, but you can do something like this:
private static bool Test(Dictionary<string, string> par = null)
{
if(par == null) par = GetMyDefaultValue();
// Custom logic here
return false;
}
You could use null
as special case, like in other answers, but if you still want to be able to call Test(null)
and have different behaviour to calling Test()
, then you must chain overload:
public void Test(Dictionary<int, bool> dic) {
//optional, stops people calling Test(null) where you want them to call Test():
if(dic == null) throw new ArgumentNullException("dic");
...
}
public void Test() {
var defaultDic = new Dictionary<int, bool>();
Test(defaultDic);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With