Reputation: 3113
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) {
...
}
Upvotes: 4
Views: 6882
Reputation: 54781
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);
}
Upvotes: 4
Reputation: 64467
Making the assumption that you want to provide a non-null
default in the method signature, you cannot do that with this type. However, you have two alternative solutions
that immediately spring to mind.
1, use optional parameters with a default, this will have to be null
for a Dictionary
(and all other reference types except string
I believe) and you will need logic inside the method to handle it:
public void Test(Dictionary<int, bool> dictionary = null)
{
// Provide a default if null.
if (dictionary == null)
dictionary = new Dictionary<int, bool>();
}
Or, and the way I'd do it, just use the "old fashioned" method overloading. This allows you to differentiate between people not providing the argument and people providing a null
argument:
public void Test()
{
// Provide your default value here.
Test(new Dictionary<int, bool>();
}
public void Test(Dictionary<int, bool> dictionary)
{
}
Optional parameters compile into overloaded methods at any rate, so they are almost semantically the same, it's just a preference as to where you wish to express your default value.
Upvotes: 0
Reputation: 10427
You can only use null
as a default parameter value for reference types.
A default value must be one of the following types of expressions:
a constant expression;
an expression of the form
new ValType()
, whereValType
is a value type, such as anenum
or astruct
;an expression of the form
default(ValType)
, whereValType
is a value type.
Upvotes: 1
Reputation: 29186
You cannot set the dictionary to anything other then NULL. If you do try, for example:
public void Test(Dictionary<int, bool> dic = new Dictionary<string, string> { { "1", "true" }})
or whatever, then you will see this error:
Default parameter value for 'dic' must be a compile-time constant.
So in that case, NULL
is your only choice. However, doing this is pointless
public void Test(Dictionary<int, bool> dic = null)
At worst the incoming dic
will be NULL
anyway, if the caller hasn't instantiated a new instance, so there is no advantage to adding the NULL
default anyway.
Upvotes: 0
Reputation: 1018
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;
}
Upvotes: 5