Optional Parameters in C# 4
Join the DZone community and get the full member experience.
Join For FreeWhen 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:
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:
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.
public List<Person> GetPeople()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.
{
GetPeople(false);
}
public List<Person> GetPeople(bool fetchAddresses)
{
// query the database and return a list of people
}
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)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.
{
// query the database and return a list of people
}
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.
csharp
Opinions expressed by DZone contributors are their own.
Comments