Skip to main content
All docs
V22.2

How to: Group and Sort Properties with Backing Fields

  • 3 minutes to read

Organize Members can group and sort properties with backing fields with the following criteria:

This example describes how to create, configure, and apply rules for properties with backing fields to implement the scenarios described above.

Keep Backing Fields with their Corresponding Properties

  1. Open the Editor | All Languages | Organize Members options page to configure the Organize Members.

    organize-members-page

  2. Choose the StyleCop rule set in the “Rule set” section.

    choose-stylecop-set

  3. Select the “Properties” rule.

    select-properties-rule

    You can change the name for this rule (optional). In this example, leave the rule name as is.

  4. Change the “Properties” rule settings.

    Set the Rule priority option to High priority to group backing fields with their properties before other rules.

    Configure grouping and sorting as shown in the screenshot below:

    change-properties-rule-settings

    For information on how to use the filter editor in the “Group by” section, refer to the following example: How to: Create a Rule.

  5. Click Apply and OK to save the changes and close the Organize Members options page.

  6. Copy the following code and paste it in your project.

    namespace Proj
    {
        public class Sample
        {
            string fieldA;
            int fieldB;
            string fieldC;
            int fieldD;
            string fieldE;
            public string PropertyA {
                get {
                    return fieldA;
                }
                set {
                    fieldA = value;
                }
            }
            internal int PropertyB {
                get {
                    return fieldB;
                }
                set {
                    fieldB = value;
                }
            }
            public string PropertyC {
                get {
                    return fieldC;
                }
                set {
                    fieldC = value;
                }
            }
        }
    }
    
  7. Run Organize members.

    Place the caret in a class member in the code above, press Ctrl + . or Ctrl + ~ to invoke the Code Actions menu, select Organize Members, and press Enter.

    properties-with-backing-fields

CodeRush places backing fields with their corresponding properties.

Group Properties with Backing Fields by Visibility

  1. Open the Organize Members options page. See the step 1 in the previous section.

  2. Select the “Internal properties” rule in the “Default” rule set.

    select-internal-properties-rule

    You can change the name for this rule (optional). In this example, do not change the rule name.

  3. Change the “Internal properties” rule settings as shown in the screenshot below.

    modify-internal-properties-rule

  4. Select the “Public properties” rule and change its settings similarly. The modified rule looks as follows:

    change-public-properties-rule

  5. Click Apply and OK to save the changes and close the Organize Members options page.

  6. Copy the code from the previous section (see the step 6) and paste it in your project.

  7. Run Organize members.

    Place the caret in a class member in the code above, press Ctrl + . or Ctrl + ~ to invoke the Code Actions menu, select Organize Members, and press Enter.

    properties-with-backing-fields-and-visibility

CodeRush places properties with backing fields according to their visibility.