The requested page is not available for the requested platform. You are viewing the content for Default platform.

Method to Property

  • 2 min to read

Creates a property from the current method.

Method to Property is a cascading refactoring. That is, the refactoring affects all method calls and method declarations in interfaces, base and descendant classes. For instance, if you apply the refactoring to the method declaration within an interface, it also changes the appropriate method declarations in all implementors and all calls to these methods.

Purpose

If a method is used to obtain or specify a field's value with some additional processing, you can make code clearer by converting this method to a property. Method to Property makes this conversion as easy as a single keystroke.

Availability

From the context menu or via shortcuts:

  • when the edit cursor, or caret is on a method name within a method declaration or call. The method should have no parameters and should have a return value. In this case, the method is converted to a read-only property.
  • when the edit cursor, or caret is on a method name within a method declaration or call. The method shouldn't have a return value and should have a single parameter that is assigned to a field. In this case, the method is converted to a write-only property.
Note

The refactoring is available for method declarations in interfaces, base and descendant classes as well as for the declaration in the current class.

Notes

  • Method to Property automatically replaces all calls to the original method with references to the newly declared property.
  • Rename is automatically invoked for the newly declared property.

Example

private const int a = 25;
public int TestMethod()
{
    return a*2;
}
Private Const a As Integer = 25
Public Function TestMethod() As Integer
    Return a * 2
End Function

Result:

private const int a = 25;
public int TestProperty
{
    get
    {
        return a * 2;
    }
}
Private Const a As Integer = 25
Public ReadOnly Property TestProperty() As Integer
    Get
        Return a * 2
    End Get
End Property

Animation

MethodToProperty

See Also