Convert Method to Property refactoring
This refactoring helps you convert methods with return values and without parameters to properties with the corresponding get
accessor, and methods with void
return type and exactly one parameter to properties with the corresponding set
accessor.
tip
The reverse functionality is available with the Convert Property to Method(s) refactoring refactoring.
Consider the following example:
Before refactoring
class Student{ private string _faculty; public string GetFaculty() { return _faculty; } public void SetFaculty(string f) { _faculty = f; }}
Applied to 'GetFaculty'
class Student{ private string _faculty; public string Faculty { get { return _faculty; } } public void SetFaculty(string f) { _faculty = f; }}
If there is an existing property with a single accessor and the converted method has compatible parameter type or return type, the refactoring can add the second accessor to the existing property:
Property with single accessor exists
class Student{ private string _faculty; public string Faculty { get { return _faculty; } } public void SetFaculty(string f) { _faculty = f; }}
Applied to 'SetFaculty'
class Student{ private string _faculty; public string Faculty { get { return _faculty; } set { _faculty = value; } }}
Place the caret at the declaration or a usage of a method in the editor, or select it in the Structure window window.
Do one of the following:
Press CtrlShift0R and then choose Convert Method to Property.
Choose Refactor | Convert Method to Property from the main menu.
The Convert Method to Property dialog will open.
Type the name for the property name leave the default suggestion. You can type the name of an existing property with a single accessor if the converted method will fit as the second accessor.
To apply the refactoring, click Next.
If no conflicts are found, JetBrains Rider performs the refactoring immediately. Otherwise, it prompts you to resolve conflicts.
tip
Right after the refactoring has been completed, all changes that it makes anywhere, including other files, are registered as a single operation. So you can use the Undo action Ctrl0Z to roll back all these changes with a single keystroke.
