Generate a constructor in Visual Studio

Applies to: yesVisual Studio noVisual Studio for Mac noVisual Studio Code

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 (C# only)

  1. Place your cursor in any empty line in a class:

    Cursor in empty line

  2. 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.

    Screenshot of the Generate constructor option.

  3. Select Generate constructor from the drop-down menu.

    The Pick members dialog box opens.

  4. Pick the members you want to include as constructor parameters. You can order them using the up and down arrows. Choose OK.

    Pick members dialog

    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.

    Screenshot showing that constructor is created with the specified parameters.

Generate constructor with properties (C# only)

  1. Place your cursor on the instance.

  2. Press Ctrl+. to trigger the Quick Actions and Refactorings menu.

  3. Select Generate constructor in <QualifiedName>(with properties).

    Screenshot of the Generate constructor in Key (with properties) option.

Generate constructor from selected fields (C# only)

  1. Highlight the members you wish to have in your generated constructor:

    Highlight members

  2. 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.

      Screenshot of the Generate constructor Person string string option.

  3. Select Generate constructor 'TypeName(...)' from the drop-down menu.

    The constructor is created with the selected parameters.

    Screenshot showing that the constructor is created with the selected parameters.

Generate constructor from new usage (C# and Visual Basic)

  1. 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#:

      Highlighted code C#

    • Visual Basic:

      Highlighted code VB

  2. 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.

      Screenshot of the Generate constructor in Person option.

  3. 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#:

      Generate method result C#

    • Visual Basic:

      Generate method result VB

Add parameter to existing constructor (C# only)

  1. Add a parameter to an existing constructor call.

  2. Place your cursor on the line where there is a red squiggle indicating you've used a constructor that doesn't yet exist.

    Screenshot showing the line where there is a red squiggle.

  3. 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.

      Screenshot of the Add parameter to Person string string option.

  4. Select Add parameter to 'TypeName(...)' from the drop-down menu.

    The parameter is added to the constructor, with its type inferred from its usage.

    Screenshot showing that 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)

  1. Find an existing constructor, and add a parameter:

    Screenshot showing an existing constructor.

  2. Place your cursor inside the newly added parameter.

  3. 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.

    Screenshot of the Create and initialize property Age option.

  4. 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.

    Screenshot showing that the field or property is declared and automatically named to match your types.

See also