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
Open the Editor | All Languages | Organize Members options page to configure the Organize Members.
Choose the StyleCop rule set in the “Rule set” section.
Select the “Properties” rule.
You can change the name for this rule (optional). In this example, leave the rule name as is.
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:
For information on how to use the filter editor in the “Group by” section, refer to the following example: How to: Create a Rule.
Click Apply and OK to save the changes and close the Organize Members options page.
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; } } } }
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.
CodeRush places backing fields with their corresponding properties.
Group Properties with Backing Fields by Visibility
Open the Organize Members options page. See the step 1 in the previous section.
Select the “Internal properties” rule in the “Default” rule set.
You can change the name for this rule (optional). In this example, do not change the rule name.
Change the “Internal properties” rule settings as shown in the screenshot below.
Select the “Public properties” rule and change its settings similarly. The modified rule looks as follows:
Click Apply and OK to save the changes and close the Organize Members options page.
Copy the code from the previous section (see the step 6) and paste it in your project.
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.
CodeRush places properties with backing fields according to their visibility.