简体   繁体   中英

How can I set default parameter value for an action

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);
}

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() , where ValType is a value type, such as an enum or a struct ;

  • an expression of the form default(ValType) , where ValType is a value type.

MSDN

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM