3

I'm going through an MVC tutorial and see this line of code at the beginning of a function:

private void PopulateDepartmentsDropDownList(object selectedDepartment = null) 

After testing it out, I can see that the function works, but I do not understand how the function parameter works. What does object selectedDepartment = null do?

I have done a general internet search and have not yet been able to locate an answer.

I guess my question really has two facets:

  1. What does the = null portion of the parameter do?
  2. Is it something that can be done but not necessarily should be done?
3
  • The = null portion of the argument makes it an optional parameter, which will supply a default value of null for that parameter. It can be valid if you want polymorphic methods that allow for optional parameters. Commented Jun 26, 2015 at 20:57
  • 1
    Nitpick on terminology - what you've shown is a parameter. The parameters are always at the place where the method is declared - the arguments are always at the place where the method is called, providing the values. It's the same for generic type parameters/type arguments - in the declaration of List<T>, T is a type parameter. When you talk about a specific concrete list type, e.g. List<string> x = new List<string>(); here string is the type argument. Commented Jun 26, 2015 at 21:00
  • It's not a nitpick if it suggests better language use. I'll update the language in the question. Commented Jun 26, 2015 at 21:02

4 Answers 4

5
  1. It means that that parameter will be null, unless you decide to pass something. So in other words, its optional.

  2. It can be done, and there is nothing wrong with it. Its pretty common practice.

Sign up to request clarification or add additional context in comments.

1 Comment

Although other answers go deeper into explaining the first part of my question, this is a compact answer and addresses both parts of my question.
2

It means that you can call

PopulateDepartmentsDropDownList() 

or

PopulateDepartmentsDropDownList("something") 

both because compiler will convert the first one to

PopulateDepartmentsDropDownList(null) 

This feature is called Optional Arguments

I suggest you to read this blog post

Comments

1

the = null is the default value of the parameter, it is the functional equivalent as if you had

private void PopulateDepartmentsDropDownList() { PopulateDepartmentsDropDownList(null); } private void PopulateDepartmentsDropDownList(object selectedDepartment) { //Your code here. } 

So if you can call the function with no parameters PopulateDepartmentsDropDownList() it will call the 1 perameter version and pass in null.

1 Comment

it is not equivalent to that, it is a compile time action and does not generate a new function signature!
0

This sets the argument to a default value (if not provided) and prevents a compile time error if the argument is not provided. See:

Setting the default value of a C# Optional Parameter

Basically this argument is now optional so you can call the function in either of these two ways:

PopulateDepartmentsDropDownList() //selectedDepartment will be set to null as it is not provided 

OR

PopulateDepartmentsDropDownList(myObject) //selectedDepartment will become myObject 

Comments