Skip to content

Latest commit

 

History

History
73 lines (50 loc) · 2.58 KB

encapsulate-field.md

File metadata and controls

73 lines (50 loc) · 2.58 KB
title description ms.date ms.topic ms.custom author ms.author manager ms.subservice f1_keywords dev_langs
Refactor a field to a property
Learn how to use the Quick Actions and Refactorings menu to convert a field into a property.
01/26/2018
reference
ide-ref
Mikejo5000
mikejo
mijacobs
general-ide
vs.csharp.refactoring.encapsulatefield
CSharp
VB

Encapsulate a field refactoring

This refactoring applies to:

  • C#

  • Visual Basic

What: Lets you turn a field into a property, and update all usages of that field to use the newly created property.

When: You want to move a field into a property, and update all references to that field.

Why: You want to give other classes access to a field, but don't want those classes to have direct access. By wrapping the field in a property, you could write code to verify the value being assigned, for example.

How-to

  1. Highlight or place the text cursor inside the name of the field to encapsulate:

    • C#:

      Highlighted code - C#

    • Visual Basic:

      Highlighted code - Visual Basic

  2. Next, do one of the following:

    • Keyboard
      • Press Ctrl+R, then Ctrl+E. (Note that your keyboard shortcut may be different based on which profile you've selected.)
      • Press Ctrl+. to trigger the Quick Actions and Refactorings menu and select either Encapsulate field entry from the Preview window popup.
    • Mouse
      • Select Edit > Refactor > Encapsulate Field.
      • Right-click the code, select the Quick Actions and Refactorings menu and select either Encapsulate field entry from the Preview window popup.
    Selection Description
    Encapsulate field (and use property) Encapsulates the field with a property, and updates all usages of the field to use the generated property
    Encapsulate field (but still use field) Encapsulates the field with a property, but leaves all usages of the field untouched

    The property is created and references to the field are updated, if selected.

    [!TIP] Use the Preview changes link in the popup window to see what the result will be before committing to it.

    • C#:

      Encapsulate Property result - C#

    • Visual Basic:

      Encapsulate Property result - Visual Basic

See also