ReSharper 2018.2 Help

Convert Method to Property refactoring

ReSharper | Refactor | Convert | Method to Property…
ReSharper_Function2Property

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.

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.

Consider the following example:

Before refactoring

Applied to 'GetFaculty'

Then applied to 'SetFaculty'

class Student { private string faculty; public string GetFaculty() { return faculty; } public void SetFaculty(string f) { faculty = f; } }

class Student { private string faculty; public string Faculty { get { return faculty; } } public void SetFaculty(string f) { faculty = f; } }

class Student { private string faculty; public string Faculty { get { return faculty; } set { faculty = value; } } }

To convert a method to a property

  1. Place the caret at the declaration or a usage of a method in the editor, or select it in the File Structure window.

  2. Do one of the following:
    • Press Ctrl+Shift+R and then choose Convert Method to Property

    • Right-click and choose Refactor | Convert Method to Property in the context menu.

    • Choose ReSharper | Refactor | Convert | Method to Property‚Ķ in the main menu.

    The Convert Method to Property dialog will open.
  3. 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.

  4. To apply the refactoring, click Next.

  5. If no conflicts are found, ReSharper performs the refactoring immediately. Otherwise, it prompts you to resolve conflicts.

Refactorings Convert Method to Property dialog box

This feature is supported in the following languages and technologies:

Language: C#

Language: VB.NET

Language: C++

Language: HTML

Language: ASP.NET

Language: Razor

Language: JavaScript

Language: TypeScript

Language: CSS

Language: XML

Language: XAML

Language: Resx

Language: Build Scripts

Language: Protobuf

Language: JSON

Feature is available

Feature is available

Feature is not available

Feature is not available

Feature is not available

Feature is not available

Feature is not available

Feature is not available

Feature is not available

Feature is not available

Feature is not available

Feature is not available

Feature is not available

Feature is not available

Feature is not available

The instructions and examples given here address the use of the feature in C#. For details specific to other languages, see corresponding topics in the ReSharper by Language section.

Last modified: 5 October 2018

See Also