The constructor generation wizard creates a non-default constructor that takes selected fields, properties and auto-properties as parameters.
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/property.
If there is a base type constructor with parameters, they are also declared as parameters of the generated constructor and passed to the base class constructor.
- Position the caret within a class where you'd like to generate a constructor.
- On the main menu, choose ReSharper | Edit | Generate Code, or press Alt+Insert.
- From the Generate pop-up menu, select Constructor.
- On the Generate constructors page of the Generate wizard, 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.
- Optionally, use the following controls:
- Access Rights defines common access rights for all constructors that the wizard generates.
- Copy XML documentation copies XML documentation from all base constructors.
- Do one of the following:
- To configure XML documentation options individually for specific constructors, click Advanced. After finishing this step, click Next.
- Otherwise, click Finish to complete the wizard. For
example, if a single base constructor and a single class member is selected, as shown
ReSharper generates the following constructor:
NoteNote that the name of generated constructor parameter
classDeclarationdiffers from the name of property
myClassDeclaration. This is accomplished by specifying
default prefixfor instance fields in Languages | Common | Naming Style tab in the ReSharper Options dialog box. This forces ReSharper to drop the prefix when generating parameters based on instance fields. Were the prefix not specified, ReSharper would generate the parameter with the same name as the initialized field, and used
this.qualifier in the initializing expression:
this.myClassDeclaration = myClassDeclaration;