ReSharper 2020.2 Help

Generate Type Constructors

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

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; } }

Generate a constructor

  1. In the editor, set the caret at the type name or within a type at the line where you want to insert generated items. If the caret is on the type name, the generated code will be added in the beginning of the type declaration.

  2. Press Alt+Insert or choose ReSharper | Edit | Generate Code… from the main menu. Alternatively, you can press Control+Shift+A, start typing the command name in the popup, and then choose it there.

  3. In the Generate popup, select Generation action.

  4. In the Generate dialog 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

    Optionally, use the following controls in the dialog:

    • Access Rights— allows you to define access rights for the generated constructor.

    • Check parameters for null (appears if the class has fields or properties of nullable types) — if this checkbox is selected, ReSharper will generate configurable null checks for each nullable parameter, for example: if (param == null) throw new ArgumentNullException(nameof(param));

    • Make parameters optional— if this checkbox is selected, ReSharper will make all parameters of the generated constructor optional and add default values corresponding to the parameter types.

  5. Click Finish to complete the wizard.
    You can also click Options to review or modify common code generation preferences on the Code Editing | Members Generation page of ReSharper options.

This feature is supported in the following languages and technologies:

Language: C#Language: VB.NETLanguage: C++Language: HTMLLanguage: ASP.NETLanguage: RazorLanguage: JavaScriptLanguage: TypeScriptLanguage: CSSLanguage: XMLLanguage: XAMLLanguage: ResxLanguage: Build ScriptsLanguage: ProtobufLanguage: JSON
Feature is available in C#Feature is available in Visual Basic .NETFeature is available in C++Feature is not available in HTMLFeature is not available in ASP.NETFeature is not available in RazorFeature is not available in JavaScriptFeature is available in TypeScriptFeature is not available in CSSFeature is not available in XMLFeature is not available in XAMLFeature is not available in Resource filesFeature is not available in build script filesFeature is not available in ProtobufFeature is not available in JSON

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.

Last modified: 03 September 2020