Back to Devexpress

Declare Menu

coderushforroslyn-118647-coding-assistance-declare-menu.md

latest5.2 KB
Original Source

Declare Menu

  • Nov 17, 2021
  • 3 minutes to read

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

The Declare menu supports the following actions:

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

How to Use

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

csharp
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:

  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.

  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.

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

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

The code snippet below shows the result:

csharp
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.

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

Example

Consider the following C# code:

csharp
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:

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

The following screencast shows the result: