ReSharper 2016.1 Help

Generating Properties

ReSharper | Edit | Generate Code | Properties / Read-only Properties
Alt+Insert | Properties / Read-only Properties
ReSharper_GenerateProperties / ReSharper_GenerateReadOnlyProperties

ReSharper will help you generate properties for all fields that you want to expose.

In the example below, this command is used to generate properties for _radius and _center fields.

Before generationAfter generation
class Circle { int _radius; readonly Point _center; public Circle(int radius, Point center) { _radius = radius; _center = center; } }
class Circle { int _radius; readonly Point _center; public int Radius { get { return _radius; } set { _radius = value; } } public Point Center { get { return _center; } } public Circle(int radius, Point center) { _radius = radius; _center = center; } }

To generate properties

  1. Set the caret within a type where you would like to generate properties.
  2. Press Alt+Insert or choose ReSharper | Edit | Generate Code… in the main menu .
  3. In the Generate pop-up menu, select Properties / Read-only Properties.
  4. In the Generate dialog box that appears, select fields to generate properties for.
    Generating properties with ReSharper
  5. Optionally, use the following controls that are applied to all generated properties:
    • Access Rights defines access rights modifiers for generated properties.
    • Read-only defines whether to generate properties as read-only or not. If you choose Automatic, ReSharper will generate only getters for read-only fields, and both getters and setters for any other fields.
      If you have selected Read-only Properties in the Generate pop-up, the selector is set to Yes, but you can change it if necessary.
    • Virtual (if applicable) adds the virtual modifier to generated properties.
    • Notify on property changes (if applicable) If your class implements the INotifyPropertyChanged or inherits from a class that supports property change notifications (e.g., Prism’s NotificationObject) and the method that notify property change is decorated with the [NotifyPropertyChangedInvocator] attribute, you can choose to implement properties with change notification calls. For more information, see INotifyPropertyChanged Support.
  6. Click Finish to complete the wizard.
    You can also click Options to review or modify commmon code generation preferences on the Code Editing | Members Generation page of ReSharper options

This command generate properties leaving the original fields intact. If necessary, you can convert the generated properties to an auto-implemented property by pressing Alt+Enter on any of the properties:

'Convert to auto-property' quick-fix

This feature is supported in the following languages/technologies:

C# VB.NET C++ HTML ASPX Razor JavaScript TypeScript CSS XML XAML RESX Build Scripts Protobuf JSON
feature_available feature_available feature_available

The instructions and examples given here address the use of the feature in C#. For details specific to other languages, see corresponding topics in the ReSharper by Language section.

See Also

Last modified: 19 August 2016