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 |
|
|
These refactorings apply to:
-
C#
-
Visual Basic
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.
-
Place your cursor in your Get method name.
-
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.
- Keyboard
-
(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.
-
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; }
}
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
-
Place your cursor in your Get method name.
-
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.
- Keyboard
-
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.