Over a million developers have joined DZone.

Optional Parameters in C# 4

When working with C#, I frequently take advantage of method overloading where I have multiple methods with the same name, but varying parameters.  Sometimes I need to be able to pass completely different data types and sometimes I just do it to allow for default options.  More often than not, it's the latter reason.  Probably the biggest area I do this is when building out data access layers.  Using my standard Person & Address data model, let's assume I have a method for GetPeople that returns a list of people.  I would then have a second method that accepts a boolean on whether or not I wanted to pre-load the address data.  The parameterless method would simply call the method with the boolean parameter with a default value:
public List<Person> GetPeople()

public List<Person> GetPeople(bool fetchAddresses)
// query the database and return a list of people
In cases where I know I only want the person data, I can call GetPeople either without a parameter or by passing in "false".  Where I do want the address data also, I have to call GetPeople and pass in "true".  If you can imagine with a more complex example, it is easy to have several methods that do nothing but call a larger method passing in the defaults. In most cases only one method will actually have any real logic.

C# 4 has a feature to help with this called "Optional Parameters" that allows you to set the default value of a parameter, thus allowing you to call the method with a shorter set of parameters.  Any parameter not specified in the call, is then set to the default.  This would allow me to build one method:
public List<Person> GetPeople(bool fetchAddresses = false)
// query the database and return a list of people
I can now call this method either with or without a parameter since I am specifying a default.  If I call it with a parameter, it will use the data I pass, otherwise fetchAddresses will be false.  While this is cool, it may not fit every purpose and I have seen several arguments on whether or not this is a good idea.  You will have to evaluate the use of the classes.  If it is something that will be internal to a project, then this can help save a lot of code.  If you are writing an API, I would recommend sticking to the old style to make the API more maintainable.  This is specific to .NET 4 so it obviously wouldn't be available with previous versions of .NET.

Optional Parameters can definitely simplify cases where you might need to use method overloads, just make sure it is the right solution based on who is consuming your classes.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}