Convert a local function to a method

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

This refactoring applies to:

  • C#

What: Convert a local function to a method.

When: You have a local function that you want to define outside your current local context.

Why: You want to convert a local function into a method so that you can call it outside your local context. You might want to convert to a method when your local function is getting too long. When you define the function in a separate method, your code is easier to read.

Convert local function to method refactoring

  1. Place your cursor in the local function.

    Convert a local function to a method code sample

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

    Convert local function to method code fix sample

  3. Press Enter to accept the refactoring.

    Convert local function to method result sample

See also