Removing a Parameter

You invoke the Change Signature refactor by positioning your cursor inside a method signature and clicking Ctrl+. (or right-clicking and choosing Quick Actions). This Refactor operation enables you to select one or more parameters from a given method, constructor, or delegate and have it (or them) removed from the method. It also allows you to reorder the parameters on the method signature. Of course, once executed, the refactoring operation updates any callers with the new method signature.


Tip

There is another keyboard shortcut to invoke the Change Signature refactoring operation directly. First, position your cursor in the method that contains the parameters you want to remove. Next, play the keyboard chord Ctrl+R, V.


Let’s look at an example. Suppose you have a method with the following signature.

public static Order GetCustomerOrder(int customerId, int orderId)

This method returns an Order object based on both a customer and an order identification number. Suppose you determine that the order ID is sufficient for returning an order. In this case, you invoke Change Signature on the method using Ctrl+. and the resulting light bulb. This brings up the Change Signature dialog, as shown in Figure 9.22. To remove a parameter, you simply select it in the dialog and click the Remove button. In this example, the customerId parameter is crossed out because it is being removed. If you change your mind, you can use the Restore button to cancel individual parameter removals.

Image

FIGURE 9.22 The Change Signature dialog in action while removing a parameter.

When you are ready to make the removal, you can choose to preview the changes or simply apply them all simultaneously. The Preview option works the same as other previews. It shows you each change in a tree view and enables you to see the details behind the change. You can, of course, also uncheck specific changes. When finished, you apply the final set of removals to your code.


Caution

It is common to declare a local variable inside a method and pass that local variable in a call to another method. If you use the refactoring operation to remove the parameter on the method you are calling, the local variable still exists in your calling method. Be careful to make sure that this is what you intended; if not, you have to remove the local variable manually.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset