Generate a method in Visual Studio

This code generation applies to:

  • C#

  • Visual Basic

What: Lets you immediately add a method to a class.

When: You introduce a new method and want to properly declare it, automatically.

Why: You could declare the method and parameters before using it, however this feature will generate the declaration automatically.

How-to

  1. Place your cursor on the line where there is a red squiggle. The red squiggle indicates a method that doesn't exist yet.

    • 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 method preview

  3. Select Generate method 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 method is created with any parameters inferred from its usage.

    • C#:

      Generate method result C#

    • Visual Basic:

      Generate method result VB

See also