Skip to main content
A newer version of this page is available. .

Declare Menu

  • 3 minutes to read

The Declare menu allows you to add a group of members to your types in C# and Visual Basic.

Declare_Main

The Declare menu supports the following actions:

Constructor
Generates a constructor with parameters that initialize the selected members.
IDisposable Implementation
Generates the Dispose pattern and disposes the specified fields and properties.
Delegate Members
Creates members that wrap around the contained field’s or property’s members.
Override Members
Overrides virtual and abstract methods.
Missing Members
Implements any interface members missing from the active class.
Partial Methods
Implements any partial methods missing from the active class.
Equality Members
Generates two Equals methods and a GetHashCode override, and can optionally generate equality/inequality operator overloads.
Comparison Members
Generates a CompareTo implementation, individually compares the specified fields, and optionally implements the IComparable<T> and IComparable interfaces.
ToString Override
Generates the ToString() override method code.
Properties/Read-only Properties
Generates properties for the selected fields.

How to Use

  1. Invoke the Declare menu. Use one of the following ways:

    • Place the caret in a member definition and press Alt+Insert.

    • Place the caret anywhere inside a type, press Ctrl + . or Ctrl + ~ to invoke the Code Actions menu, select the Declare… menu item, and press Enter.

      declare-item

    CodeRush shows the Declare menu.

    Invoke

  2. Choose the desired action from the menu (for example, Properties) and press Enter.

    Choose

  3. Use the mouse or press Space to select the members for which you want to generate properties. The following screenshot shows the selected “fahrenheit” member:

    Declare_2

  4. Optional. Tap Ctrl to open the Options menu.

  5. Configure the Options menu. Use the mouse or corresponding letter keys listed in the menu to change options.

    Options

    Tap Ctrl or press Enter to close the Options menu.

  6. Press Enter to confirm the selection and declare members.

The code snippet below shows the declared “Fahrenheit” property:

public class Temperature {

    int fahrenheit;
    int celcius;
    internal int Fahrenheit {
        get
        {
            return fahrenheit;
        }

        set
        {
            fahrenheit = value;
        }
    }
}

Configuration

You can customize how the Declare menu generates new members.

Open the CodeRush | Options | Editor | C# or Visual Basic | Code Actions | Code Actions Settings options page, and change the “Default body of newly generated methods” and “Style of newly generated properties” settings.

Page

For more information on these settings, refer to the following topic: Code Actions Settings.

Example

Consider the following C# code:

namespace ConsoleApp
{
    interface ITest
    {
        string Name { get; set; }
        void MyTest();
    }

    public class Test: ITest
    {

    }    
}

To generate a MyTest() method with a default return body and declare auto properties in the interface implementer, perform the following actions:

Open the Code Actions Settings options page and set the corresponding settings as shown in the screenshot below:

Page

Invoke the Declare menu, select Missing Members, and press Enter.

The following screencast shows the result:

screencast