Skip to main content

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

Consider the code snippet below to see the “Declare Menu” feature in action:

public class Person {  
    string firstName;  
    string lastName;  
    int age;  
}  

You can use the “firstName”, “lastName”, and “age” class fields to generate a constructor and properties code.

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

    • Place the caret anywhere inside a type and press Alt+Insert.

    • Place the caret in a type declaration, 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. Select Constructor from the menu and press Enter.

  3. Use the mouse or press Space to select fields you want to initialize in the constructor and press Enter.

    select-fields

  4. Tap Ctrl to open the Options menu to configure constructor visibility and generate properties for fields. You can use the mouse or corresponding letter keys listed in the menu to change options.

    options

  5. Press G to check the “Generate Properties” checkbox.

    generate-properties

  6. Press Enter twice to close the options menu and generate new code.

The code snippet below shows the result:

public class Person {  
    string firstName;  
    string lastName;  
    int age;  
    public Person(string firstName, string lastName, int age) {  
        this.firstName = firstName;  
        this.lastName = lastName;  
        this.age = age;  
    }  
    public string FirstName {  
        get {  
            return firstName;  
        }  
    }  
    public string LastName {  
        get {  
            return lastName;  
        }  
    }  
    public int Age {  
        get {  
            return age;  
        }  
    }  
}

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