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
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; }
}
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
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.
See also
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for