ReSharper 2024.2 Help

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.

Consider the following example:

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; } }

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:

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; } } }

Convert a method into 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 window.

  2. Do one of the following:

    • Press Control+Shift+R and then choose Convert Method to Property.

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

    • Choose ReSharper | Refactor | Convert | Method to Property… from 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.

ReSharper refactorings: Convert method to property dialog

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 in C#

Feature is available in Visual Basic

Feature is not available in C++

Feature is not available in HTML

Feature is not available in ASP.NET

Feature is not available in Razor

Feature is not available in JavaScript

Feature is not available in TypeScript

Feature is not available in CSS

Feature is not available in XML

Feature is not available in XAML

Feature is not available in Resource files

Feature is not available in build script files

Feature is not available in Protobuf

Feature is not available in JSON

The instructions and examples given here address the use of the feature in C#. For more information about other languages, refer to corresponding topics in the ReSharper by language section.

Last modified: 23 September 2024