Skip to content

Latest commit

 

History

History
88 lines (61 loc) · 2.52 KB

convert-get-method-to-property.md

File metadata and controls

88 lines (61 loc) · 2.52 KB
title description ms.date ms.topic ms.devlang author ms.author manager ms.subservice f1_keywords dev_langs
Convert Get method to or from a property
Learn how to use the Quick Actions and Refactorings menu to convert a Get method (and optionally your Set method) into a property.
03/10/2020
reference
csharp
mikadumont
midumont
mijacobs
general-ide
vs.csharp.refactoring.convertmethodtoproperty
CSharp
VB

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