Convert Get method to property / Convert property to Get method refactorings

These refactorings apply to:

  • C#

  • Visual Basic

Convert Get method to property

What: Lets you convert a Get method into a property (and optionally your Set method).

When: You have a Get method that does not contain any logic.

How-to

  1. Place your cursor in your Get method name.

  2. Next, do one of the following:

    • Keyboard
      • Press Ctrl+. to trigger the Quick Actions and Refactorings menu, and select Replace method with property from the Preview window popup.
    • Mouse
      • Right-click the code, select the Quick Actions and Refactorings menu, and select Replace method with property from the Preview window popup.
  3. (Optional) If you have a Set method, you can also convert your Set method at this time by selecting Replace Get method and Set method with property.

  4. If you are happy with the change in the code preview, press Enter or click the fix from the menu and the changes will be committed.

Example:

private int MyValue;

// Before
public int GetMyValue()
{
    return MyValue;
}

// Replace 'GetMyValue' with property

// After
public int MyValue
{
    get { return MyValue; }
}

Convert property to Get method

What: Lets you convert a property to a Get method

When: You have a property that involves more than immediately setting and getting a value

How-to

  1. Place your cursor in your Get method name.

  2. Next, do one of the following:

    • Keyboard
      • Press Ctrl+. to trigger the Quick Actions and Refactorings menu and select Replace property with methods from the Preview window popup.
    • Mouse
      • Right-click the code, select the Quick Actions and Refactorings menu and select Replace property with methods from the Preview window popup.
  3. If you are happy with the change in the code preview, press Enter or click the fix from the menu and the changes will be committed.

See also