0

I came across an example similar to this:

public Dictionary<string, object> generate(
  string elementId,
  Dictionary<string, object> additionalAttributes = null)
{
.... method body
}

Why would the dictionary passed as parameter be initiated to null? I haven't seen such construct. Does it have to do something with an optional parameter?

Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104
Turo
  • 1,537
  • 2
  • 21
  • 42
  • 2
    `Does it have to do something with an optional parameter` - correct. The caller can then pass in `elementId` and not have to explicitly include `null` for the 2nd parameter. – Igor Jul 01 '16 at 14:01
  • See also: [optional arguments](https://msdn.microsoft.com/library/dd264739.aspx) – Corak Jul 01 '16 at 14:02
  • side note: `IDictionary additionalAttributes = null` (pleasem notice `I` - interface i.e. any class that can be used as dictionary) will be a better solution – Dmitry Bychenko Jul 01 '16 at 14:06
  • It's optional as others have noted. Is there conditional logic in the method that tests for null? That would make sense. – Big Daddy Jul 01 '16 at 14:06

4 Answers4

3

I can't speak to your first question, but the answer to your second question is yes. This is an optional parameter. C# only allows optional reference-type parameters to take a default value of null, except for string, which can take any constant string value, I believe.

Ref: MSDN

Dan Forbes
  • 2,734
  • 3
  • 30
  • 60
1

I use that to save time writing functions overloading. For example, instead of overloading two functions:

void SameFunctionName(Parameter1){ .. }
void SameFunctionName(Parameter1, Parameter2){ .. }
// maybe additional function body with three parameters .. etc

I just write one using this case:

void MyFunction(Parameter1, Parameter2 = null){ .. }

So, a small if statement inside my function would check if Parameter2 is null or not, to then make decisions. All in one function body.

and the function call for this case would work in both cases:

MyFunction(Parameter1); // This is a valid syntax
MyFunction(Parameter1, Parameter2); // This is a valid syntax
Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104
0

Optional parameter such as the one you use in your example can only be set to constant values, this means that you can't use any reference values, which is what Dictionary is, as such null is the only allowed value you can initialise an optional variable of type Dictionary to, if the method was using a value type like int or string then a value could be initialised for the optional parameter otherwise it has to be null

MikeT
  • 5,398
  • 3
  • 27
  • 43
  • `string` is not a value type in C#. Ref: [StackOverflow](http://stackoverflow.com/questions/1069155/is-string-a-value-type-or-a-reference-type). – Dan Forbes Jul 01 '16 at 14:16
  • @Dan how could you include hyper links in your comment? – Khalil Khalaf Jul 01 '16 at 14:21
  • @FirstStep - StackOverflow supports Markdown. Ref: [StackOverflow](http://stackoverflow.com/editing-help) #Meta – Dan Forbes Jul 01 '16 at 14:28
  • @Dan Got it. [Thanks](https://www.google.com/search?q=thanks&rlz=1C1GIVA_enUS679US679&espv=2&biw=1536&bih=758&source=lnms&tbm=isch&sa=X&ved=0ahUKEwiCld3IvdLNAhWH7IMKHc1ZB9kQ_AUIBigB#tbm=isch&q=thanks+gif) – Khalil Khalaf Jul 01 '16 at 14:31
  • @DanForbes string is a rather odd one that isn't easily categorised as it's reference type but can be used in places that normally only allow value types, such as constants, i didn't think that trying to explain that would add an value to the answer though – MikeT Jul 01 '16 at 15:41
0

Yes it saves time if you are using Function Overloading For example this can be avoided

Void Main()
        {
          fun1(11);
          fun1(12,13);
        }

    public fun1(int i )
    {
    Print(i);
    }

    public fun1(int i ,int j)
    {
    Print(i+j);
    }

This can be avoided by Code below and it also saves time and space

Void Main()
{
fun1(12);
}

public fun1(int i ,int j = NULL)
{
if(j==NULL)
  Print(i);
else
Print(i+j);
}