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 (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 screwdriver icon that appears in the left margin if the text cursor is already on the empty line in the class.

    Generate constructor preview

  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.

    Generate constructor result

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

    Generate Constructor preview

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 screwdriver icon that appears in the left margin if the text cursor is already on the line with the selection.

      Generate Constructor preview

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

    The constructor is created with the selected parameters.

    Generate Constructor result

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 error light bulb icon that appears.
      • Click the error light bulb icon that appears in the left margin if the text cursor is already on the line with the red squiggle.

      Generate constructor preview

  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.

    Generate constructor highlight

  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 error light bulb icon that appears.
      • Click the error light bulb icon that appears in the left margin if the text cursor is already on the line with the red squiggle.

      Generate constructor preview

  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.

    Generate constructor result

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:

    Generate constructor highlight

  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 screwdriver icon that appears in the left margin if the text cursor is already on the line with the added parameter.

    Generate constructor preview

  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.

    Generate constructor result

See also