Add Parameter
- 4 minutes to read
#Purpose
Adds a new parameter to a method signature and update all method references.
Note
Add Parameter is a cascading Code Provider. That means that the Code Provider affects all method calls and method declarations in interfaces, base and descendant classes.
#Availability
Available when the cursor is in a method signature or in the arguments list in the method reference.
#Usage
Place the caret in a method signature.
Note
The blinking cursor shows the caret’s position at which the Code Provider is available.
- Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.
- Select Add Parameter from the menu.
- Specify the type of new parameter and press Enter.
- For the base method and all overridden implementations, specify the parameter name. Use the Enter key to switch between method overrides.
- For each method reference, specify what is passed to the method as a newly added parameter. Use the Enter key to switch between method usages.
After execution, the Code Provider adds a new method parameter and updates all method usages.
static double Process(List<int> data, double shift) {
// ...
return data.Average();
}
static void Main() {
var dataset1 = new List<int> { /* ... */};
var dataset2 = new List<int> { /* ... */};
var result1 = Process(dataset1, 2.4);
var result2 = Process(dataset2, -1.8);
// ...
}
Note
Being called from the method arguments list, this Code Provider determines the parameter type automatically, according to the passed argument type.
#Adding Parameters Passed by Reference
You can also specify the out or ref keyword if the parameter should be passed onto a method by reference. Consider the code snippet below.
public static double Process(List<int> data, double shift│){
// ...
return data.Average() + shift;
}
static void Main() {
// ...
var result1 = Process(dataset1, 2.4);
// ...
var result2 = Process(dataset2, -1.8);
// ...
}
Follow the instructions from the Usage section to add a new parameter and specify the “ref int“ string as a new parameter’s type (add ByRef in VB). If the out or ref keyword was specified, CodeRush passes the corresponding variable on method usages and declares/initializes it if required.