Create an overloaded delegate quickly

Sometimes it is necessary to create method overloads that just pass default values to other overloads. ReSharper allows you to do this quickly and easily.

Take this simple method for example:

screenshot_01.jpg

We need to take the CreateUser method and add a new parameter to it. However, we also need to keep the old CreateUser method signature around for other code that still uses that version of the method signature. Using ReSharper’s Change Signiture (Control-F6) feature will allow us to create an overloaded method to solve our problem.

screenshot_02.jpg

When the Change Signature dialog opens for this method, we will go ahead and add our new email parameter to this method.

screenshot_03.jpg

In the above screenshot, notice the option “Delegate via overloading method” circled in red. Clicking this box is what will take the signature changes and turn them into a new overloaded method.

When the changes are ready, clicking Next in the Change Signature dialog will now produce the following code.

screenshot_04.jpg

Notice that in the SaveNewUser method, the original method signature remains intact, but it is now delegated through an overloaded method. The CreateUser method now has two overloaded variations, the delegating method and the main method, where we can now add code to handle the new email parameter.

Jeff Pennal
JetBrains .NET Evangelist

This entry was posted in How-To's, ReSharper Tips&Tricks and tagged . Bookmark the permalink.

2 Responses to Create an overloaded delegate quickly

  1. “Delegate” seems like a poor choice of works for something that generates .NET code but has nothing to do with a .NET delegate.

    “Create new CreateUser overload and call selected CreateUser” seems like it better describes what will happen without using the much overloaded word “delegate”.

  2. Yuriy says:

    What about creating overloaded metod by removing a parameter and calling old versions with default parameter value?

    Last R# 4 builds create overload n wrong direction (as for me).

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>