Declare Parameter
- 3 minutes to read
Purpose
Generates a method parameter for an undeclared identifier and adds it to the method parameter list. This code provider also links the declared parameter and identifier.
You can also run this code provider inside a method to add the new parameter from the place where it is referenced.
Note
Declare Parameter is a cascading code provider. It affects all method calls and method declarations in interfaces and base and descendant classes.
Availability
Available when the caret is in an undeclared identifier name.
Usage
Place the caret in the undeclared identifier (the “source” identifier in this example).
public class Sample { public static double ConvertToUSD(double amount) { return amount / GetCurrencyRate(source); } protected static double GetCurrencyRate(Currency source) { throw new NotImplementedException(); } static void Main(string[] args) { double price = ConvertToUSD(10); } } public enum Currency { USD, EUR, GBR }
Press Ctrl + . or Ctrl + ~ to invoke the Code Actions menu.
Select Declare | Parameter from the menu and press Enter.
After execution, the code provider adds the new method parameter named after the identifier and a default value of the corresponding type to each method call.
public class Sample
{
public static double ConvertToUSD(double amount, Currency source)
{
return amount / GetCurrencyRate(source);
}
protected static double GetCurrencyRate(Currency source)
{
throw new NotImplementedException();
}
static void Main(string[] args)
{
double price = ConvertToUSD(10, Currency.USD); // The first enum item is used.
}
}
public enum Currency
{
USD,
EUR,
GBR
}
This code provider also links identifiers. If you change a linked identifier reference, CodeRush applies this change to other identifier references.
Blazor Support
The Declare Parameter code provider is available from the @code section of .razor files: