ReSharper 10.0 Help

Generating Type Constructors

ReSharper | Edit | Generate Code | Constructor
Alt+Insert | Constructor
ReSharper_GenerateConstructor

The constructor generation wizard creates a non-default constructor that takes parameters for selected fields , properties and auto-properties .

All generated constructors follow the same pattern where:

  • Each field , property, or auto-property included in the constructor is initialized with a parameter.
  • The name of the parameter is derived from the name of the corresponding field or property .

If there are non-default base type constructors, the required parameters are added to the generated constructor and passed to the base class constructor.

In the example below, this command is used to generate a new Circle constructor that takes two additional parameters to initialize _radius and _center fields.

Before generationAfter generation
internal class Shape { public Shape(Color color) { Color = color; } public Color Color { get; } } class Circle : Shape { int _radius; Point _center; public Circle(Color color) : base(color) { } }
internal class Shape { public Shape(Color color) { Color = color; } public Color Color { get; } } class Circle : Shape { int _radius; Point _center; public Circle(Color color) : base(color) { } public Circle(Color color, int radius, Point center) : base(color) { _radius = radius; _center = center; } }

To generate a constructor

  1. Set the caret within a type where you would like to generate a constructor.
  2. Press Alt+Insert or choose ReSharper | Edit | Generate Codeā€¦ in the main menu .
  3. In the Generate pop-up menu, select Constructor.
  4. In the Generate dialog box that appears, select type members that should be initialized in the new constructor. Optionally, select one or several base class constructors. For every selected base constructor, a new constructor will be generated that will call the base and additionally initialize selected members.
    Generating type constructors with ReSharper
  5. Optionally, use the Access Rights selector to define access rights for all generated constructors.
  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 feature is supported in the following languages/technologies:

C# VB.NET C++ HTML ASPX Razor JavaScript TypeScript CSS XML XAML RESX Build Scripts Protobuf
feature_available 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: 8 January 2016