Generate a constructor in Visual Studio
This code generation applies to:
C#
Visual Basic
What: Lets you immediately generate the code for a new constructor on a class.
When: You introduce a new constructor and want to properly declare it automatically, or you modify an existing constructor.
Why: You could declare the constructor before using it, however this feature will generate it, with the proper parameters, automatically. Furthermore, modifying an existing constructor requires updating all the callsites unless you use this feature to update them automatically.
How: There are several ways to generate a constructor:
- Generate constructor and pick members
- Generate constructor with properties
- Generate constructor from selected fields
- Generate constructor from new usage
- Add parameter to existing constructor
- Create and initialize field/property from a constructor parameter
Generate constructor and pick members (C# only)
Place your cursor in any empty line in a class:
Next, do one of the following:
- Keyboard
- Press Ctrl+. to trigger the Quick Actions and Refactorings menu.
- Mouse
- Right-click and select the Quick Actions and Refactorings menu.
- Click the icon that appears in the left margin if the text cursor is already on the empty line in the class.
- Keyboard
Select Generate constructor from the drop-down menu.
The Pick members dialog box opens.
Pick the members you want to include as constructor parameters. You can order them using the up and down arrows. Choose OK.
Tip
You can check the Add null checks checkbox to automatically generate null checks for your constructor parameters.
The constructor is created with the specified parameters.
Generate constructor with properties (C# only)
Place your cursor on the instance.
Press Ctrl+. to trigger the Quick Actions and Refactorings menu.
Select Generate constructor in
<QualifiedName>
(with properties).
Generate constructor from selected fields (C# only)
Highlight the members you wish to have in your generated constructor:
Next, do one of the following:
Keyboard
- Press Ctrl+. to trigger the Quick Actions and Refactorings menu.
Mouse
- Right-click and select the Quick Actions and Refactorings menu.
- Click the icon that appears in the left margin if the text cursor is already on the line with the selection.
Select Generate constructor 'TypeName(...)' from the drop-down menu.
The constructor is created with the selected parameters.
Generate constructor from new usage (C# and Visual Basic)
Place your cursor on the line where there is a red squiggle. The red squiggle indicates a call to a constructor that doesn't yet exist.
C#:
Visual Basic:
Next, do one of the following:
Keyboard
- Press Ctrl+. to trigger the Quick Actions and Refactorings menu.
Mouse
- Right-click and select the Quick Actions and Refactorings menu.
- Hover over the red squiggle and click the icon that appears.
- Click the icon that appears in the left margin if the text cursor is already on the line with the red squiggle.
Select Generate constructor in 'TypeName' from the drop-down menu.
Tip
Use the Preview changes link at the bottom of the preview window to see all of the changes that will be made before making your selection.
The constructor is created, with any parameters inferred from its usage.
C#:
Visual Basic:
Add parameter to existing constructor (C# only)
Add a parameter to an existing constructor call.
Place your cursor on the line where there is a red squiggle indicating you've used a constructor that doesn't yet exist.
Next, do one of the following:
Keyboard
- Press Ctrl+. to trigger the Quick Actions and Refactorings menu.
Mouse
- Right-click and select the Quick Actions and Refactorings menu.
- Hover over the red squiggle and click the icon that appears.
- Click the icon that appears in the left margin if the text cursor is already on the line with the red squiggle.
Select Add parameter to 'TypeName(...)' from the drop-down menu.
The parameter is added to the constructor, with its type inferred from its usage.
You can also add a parameter to an existing method. For more information, see Add parameter to a method.
Create and initialize a field or property from a constructor parameter (C# only)
Find an existing constructor, and add a parameter:
Place your cursor inside the newly added parameter.
Next, do one of the following:
- Keyboard
- Press Ctrl+. to trigger the Quick Actions and Refactorings menu.
- Mouse
- Right-click and select the Quick Actions and Refactorings menu.
- Click the icon that appears in the left margin if the text cursor is already on the line with the added parameter.
- Keyboard
Select Create and initialize property or Create and initialize field from the drop-down menu.
The field or property is declared and automatically named to match your types. A line of code is also added to initialize the field or property in the constructor body.