Skip to main content

Declare Method

  • 3 minutes to read

Purpose

Generates a method with appropriate parameters for an undeclared method call. You can use this code provider in Test-Driven Development.

screencast

The Declare Method provider adds a method declared in the interface to all its implementers.

screencast2

This provider can also drop a marker onto the method call if the Marker feature is enabled. See the following topic section for more details: Markers: How to Enable.

Availability

Available when the caret is in an undeclared method.

Usage

  1. Place the caret in an undeclared method’s name (“GetCurrencyRate” in this example).

    public class Sample 
    {
        public static double ConvertToUSD(double amount, object source)
        {
            return amount / GetCurrencyRate(source);
        }
        static void Main(string[] args)
        {
            double price = ConvertToUSD(10, null);
        }
    }
    
  2. Press Ctrl + . or Ctrl + ~ to invoke the Code Actions menu.

    menu

  3. Select Declare Method from the menu and press Enter. A red target picker marker appears that allows you to choose the place where the generated code can be inserted.

    red-picker-marker

    You can configure the Target Picker on the Editor | All Languages | Code Actions | Target Picker options page.

    TargetPicker

  4. Use the Up Arrow and Down Arrow keys to move the target picker.

  5. Press Enter to generate a code in the selected place.

After execution, this code provider adds the method declaration to the class/interface. In the example below, the Declare Method provider added the not-implemented GetCurrencyRate () method to the Sample class:

public class Sample 
{
    public static double ConvertToUSD(double amount, object source)
    {
        return amount / GetCurrencyRate(source);
    }
    static void Main(string[] args)
    {
        double price = ConvertToUSD(10, null);
    }
    static double GetCurrencyRate(object source)
    {
        throw new NotImplementedException();
    }
}

Blazor Support

The Declare Method code provider is available from the @code section and markup of .razor files:

declare-method-in-blazor

Note

If a Razor code-behind file (.razor.cs) exists, the Declare Method code provider adds the declared method to this file instead of the @code section.

Customization

Change Code Actions Settings

You can configure the default body of newly-generated methods on the Editor | C# (Visual Basic)| Code Actions | Code Actions Settings options page. The default value is “Throw NotImplementedException instance”.

code-actions-settings

Refer to the following topic for more information: Code Actions Settings.

Change Scope

You can change the default visibility modifier of a declared method on the Editor | C# (Visual Basic) | Scope Options options page.

Options_Scope

For example, set the default scope for this method to “Protected”. The code provider declares a method with the protected visibility, as shown below:

protected static double GetCurrencyRate(object source)
{
    throw new NotImplementedException();
}

See the following topic for details: Scope.