Home > Refactorings > How to create a method or constructor overload using CodeRush

How to create a method or constructor overload using CodeRush

July 31st, 2012

The process of creating more than one method in a class with the same name is called as method overloading. Method overloading allows the developer to define several methods with the same name but with a different set of parameters. This way, one does not have to remember the names of multiple functions that serve a similar core purpose. When a call is made to one of these overloaded methods, the compiler automatically determines which of the methods should be used according to the arguments used in the call.

For method overloading purposes, you can utilize the Create Overload refactoring. Once you apply the refactoring on a method:

CodeRush Create Overload popup menu

you can choose the destination of the new overload with the target picker:

CodeRush Create Overload target picker

When you are done with target selection, press Enter to commit. Now, you are able to choose what parameters you want to remove from the initial method to create a new overload. Initially, the newly declared method has the same signature as the source method:

CodeRush Create Overload initial method

While overloading methods, a rule to follow is that overloaded methods must differ, either in the number of arguments they take or the data type of at least one argument. The refactoring allows you to remove any number of parameters to create a new overload. If you want to add an additional parameter, use the Add Parameter refactoring for this purpose.

The shortcuts hint will help you with the available keyboard keys used to remove parameters. You can use arrow keys to select a parameter, and the Space bar to remove the currently selected parameter. The preview hint will illustrate the resulting method overload declaration, which parameters will be removed, and how the parameters will be replaced with the local declarations initialized to default values:

CodeRush Create Overload new overload

You can also create overloaded constructors as well as methods. In this case, the additional constructors are simply added to the code with a call to the source constructor:

CodeRush Create Overload new constructor overload

The XML documentation attached to a member is also updated correspondingly.

Products: CodeRush Pro
Versions: 12.1 and up
VS IDEs: 2008 and up
Updated: Aug/09/2012
ID: R059

Similar Posts:

  1. No comments yet. Be the first and leave a comment!