Inline Recent Assignment

Replaces the identifier at the caret with its most recent assignment.

Purpose

This refactoring can help you simplify your code by removing unnecessary temporary variables.

Availability

Available from the context menu or via shortcuts:

  • when the caret is on an variable or a property. There should be an assignment to this variable or property prior to the current statement.

Note

  • The assignment statement is deleted if Inline Recent Assignment is used for a local variable, and there are no further references to this variable.

Example

private string field1;
private string field2;
private void TestMethod()
{
    int s = 1;
    field1 = "field value - " + s;
    s = 22;
    field2 = "field value - " + s;
}
Private field1 As String
Private field2 As String
Private Sub TestMethod()
    Dim s As Integer = 1
    field1 = "field value - " + s
    s = 22
    field2 = "field value - " + s
End Sub

Result:

private string field1;
private string field2;
private void TestMethod()
{
    int s = 1;
    field1 = "field value - " + s;
    field2 = "field value - " + 22;
}
Private field1 As String
Private field2 As String
Private Sub TestMethod()
    Dim s As Integer = 1
    field1 = "field value - " + s
    field2 = "field value - " + 22
End Sub

Screenshot

rsInlineRecentAssignment

See Also