Skip to main content
A newer version of this page is available. .

Widen Scope (promote to field)

  • 2 minutes to read

Purpose

Converts a variable declared within a method to a private field.

Availability

Available on a variable declaration within a method.

Usage

  1. Place the caret on a variable declaration.

    NOTE

    The blinking cursor shows the caret's position at which the Refactoring is available.

    class TestClass {
        private void SetUrl(string url) {
            string completeUrl = string.Format("http://{0}", url);
        }
    }
    
  2. Use the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions Menu.
  3. Select Widen Scope (promote to field) from the menu.

After execution, the Refactoring moves the variable declaration out of the current methods and converts it to a field.

class TestClass {
    string completeUrl;
    private void SetUrl(string url) {
        completeUrl = string.Format("http://{0}", url);
    }
}
NOTE

The default visibility modifier of the generated member is configured on the Editor | <Language> | Scope Options options page. Refer to the Scope topic for details.

See Also