Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

April 11, 2010

Optional Parameters

Filed under: C#,VB.NET @ 7:19 pm

One of the new features in C# 4.0 is optional parameters, which is the ability to define a function parameter with a default value. When the function is called, the caller can optionally define the argument for the parameter or leave it off. If it is not defined, the default value is used.

(VB has had optional parameters since the beginning of time. The examples here show both C# and VB for completeness.)

Say you have two overloads of a function:

In C#:

public Customer Retrieve(int id)
{
}

public Customer Retrieve(string name)
{
}

In VB:

Public Function Retrieve(ByVal id As Integer) As Customer

End Function

Public Function Retrieve(ByVal name As String) As Customer

End Function

The first function retrieves a customer by Id. The second function retrieves a customer by name.

Now a new feature of your application requires that both functions optionally return address information. One way to accomplish this requirement is to add another set of function overloads, resulting in the following code.

In C#:

public Customer Retrieve(int id)
{
}

public Customer Retrieve(string name)
{
}

public Customer Retrieve(int id, bool includeAddress)
{
}

public Customer Retrieve(string name, bool includeAddress)
{
}

In VB:

Public Function Retrieve(ByVal id As Integer) As Customer

End Function

Public Function Retrieve(ByVal name As String) As Customer

End Function

Public Function Retrieve(ByVal id As Integer,
              ByVal includeAddress As Boolean) As Customer

End Function

Public Function Retrieve(ByVal name As String,
              ByVal includeAddress As Boolean) As Customer

End Function

You have basically doubled the number of function overloads. Now imagine that you can also retrieve by customer number or date. You could end up with many overloads.

Another approach to adding this parameter is to use the optional parameter feature. A parameter becomes an optional parameter simply by defining a default value. (VB requires the Optional keyword as well.)

NOTE: All optional parameters must be at the end of the parameter  list, after all required parameters.

In C#:

public Customer Retrieve(int id, bool includeAddress = true)
{
}

public Customer Retrieve(string name, bool includeAddress = true)
{
}

In VB:

Public Function Retrieve(ByVal id As Integer,
      Optional ByVal includeAddress As Boolean = True) As Customer

End Function

Public Function Retrieve(ByVal name As String, 
     
Optional ByVal includeAddress As Boolean = True) As Customer

End Function

In the above examples, the includeAddress parameter now has a default value. The default must be a constant. If the calling code does not pass in the second parameter, the code will assign the value to the default, in this case True.

You can call the parameter using any of the following.

In C#:

Customer cust = new Customer();
cust.Retrieve(3, true);
cust.Retrieve("Acme Inc", false);
cust.Retrieve(3);

In VB:

Dim cust As New Customer
cust.Retrieve(3, True)
cust.Retrieve("Acme Inc", False)
cust.Retrieve(3)

The first two function calls pass the parameter with a specific value. The last function call makes use of the optional parameter. Since the parameter was not specified, it’s value is set to the default value which in this case is true.

Intellisense shows the parameter as option using the customary square braces ([]).

image

Use this technique whenever you have a parameter that is optional and can have a default value. You can use this technique on a method, constructor, indexer, or delegate.

Enjoy!

EDIT 4/12/10: As Matt pointed out in the comments below, the ability to define an optional parameter on a delegate is C# only. I have expanded on this issue in another post here.

2 Comments

  1.   Matt Sedlak — April 12, 2010 @ 11:54 am    Reply

    The following URL states “Optional parameters may not be specified in delegate or event declarations.”

    http://msdn.microsoft.com/en-us/library/aa711959(VS.71).aspx

    The following URL states “ParamArray parameters may not be specified in delegate or event declarations.”

    http://msdn.microsoft.com/en-us/library/aa711960(VS.71).aspx

    Is your last statement above an indication that either of these restrictions is changing in VS2010?

  2.   DeborahK — April 13, 2010 @ 12:30 am    Reply

    Hi Matt –

    You are correct in that optional parameters cannot be specified in a delegate in VB. However, they can be specified in a delegate in C#. To clarify, I have edited this post and defined a new post specifically on this topic:

    http://msmvps.com/blogs/deborahk/archive/2010/04/12/optional-parameters-and-delegates.aspx

    This post did not refer to ParamArray parameters.

    Hope this clarifies this topic.

RSS feed for comments on this post. TrackBack URI

Leave a comment

*

© 2014 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs