Extract Property
- 3 minutes to read
In This Article
Moves the selected code to a new read-only property. Inserts the appropriate calling code into the source method or property.
#Purpose
Extract Property is great when you find you have an expression that you are using a lot, as it will take a chunk of code and move it into a new read-only property. Turning an often-used expression into a property eliminates code duplication and makes the code easier to read.
#Availability
Available from the context menu or via shortcuts:
- when one or more statements are selected.
- when an expression is selected.
Using the Intelligent Paste feature:
- When a qualifying code block is cut from a method or property and subsequently pasted inside a struct or class.
Note: availability is identical to Extract Method, with one additional constraint - the extracted block must not require any parameters, and must change only one local variable (which must be referenced after the selected block).
#Notes
- You can choose the newly declared property’s location using the target picker (see Settings below).
- Immediately after Extract Property, Rename becomes enabled so you can provide an appropriate name for the extracted property.
- If you extract the right-hand side of an assignment, the Extract Property automatically suggests a property name based on the assignment’s left-hand side.
#Tips
- If the extracted property’s return value is assigned to a variable that is referred but not changed later, you can use Inline Temp refactoring to replace occurrences of the variable with the extracted property call. This will eliminate a redundant local variable.
- The Extract Property refactoring can be automatically called by the Replace Temp with Query refactoring. Refer to documentation on the Replace Temp with Query refactoring for additional information.
- Extract Method and Introduce Local refactorings are similar to Extract Property in functionality.
#Options
- You can choose whether the extracted property should be located above or below the source method or property or whether its position should be set manually with the target picker.
- You can specify whether the extracted expression should be embedded inside parentheses.
- You can control whether the Intelligent Paste feature is available.
#Example
private string Name;
private string GetName()
{
return Name;
}
private string TestMethod()
{
return │"Name:" + GetName();
}
Private Name As String
Private Function GetName() As String
Return Name
End Function
Private Function TestMethod() As String
Return │"Name: " + GetName()
End Sub
Result:
private string Name;
private string GetName()
{
return Name;
}
private string PropName
{
get
{
return "Name:" + GetName();
}
}
private string TestMethod()
{
return PropName;
}
Private Name As String
Private Function GetName() As String
Return Name
End Function
Private ReadOnly Property PropName() As String
Get
Return "Name: " + GetName()
End Get
End Property
Private Function TestMethod() As String
Return PropName
End Sub
#Animation
See Also