Skip to main content

Inline Method

  • 2 minutes to read

Replaces the current method calls with the method’s body.

If the refactoring is called for a method call, it only replaces the initial method call. If it is called for the method declaration, it replaces all method calls.

#Availability

Available from the context menus or via shortcuts:

  • when the edit cursor or caret is on the method name within its call or declaration.

#Example

private List<UserInfo> GetUsersElderThen(int minAge)
{
    int maxAge = int.MaxValue;
    return FindUsers(minAge, maxAge);
}
private List<UserInfo> FindUsers(int minAge, int maxAge)
{
    List<UserInfo> result = new List<UserInfo>();
    foreach (UserInfo uInfo in _Users)
    {
        if (uInfo.Age >= minAge && uInfo.Age <= maxAge)
            result.Add(uInfo);
    }
    return result;
}
Private Function GetUsersElderThen(minAge As Integer) As List(Of UserInfo)
    Dim maxAge As Integer = Integer.MaxValue
    Return FindUsers(minAge, maxAge)
End Function
Private Function FindUsers(minAge As Integer, maxAge As Integer) As List(Of UserInfo)
    Dim result As List(Of UserInfo) = New List(Of UserInfo)
    For Each uInfo In _Users
        If uInfo.Age <= maxAge And uInfo.Age >= minAge Then
            result.Add(uInfo)
        End If
    Next
    Return result
End Function

Result:

private List<UserInfo> GetUsersElderThen(int minAge)
{
    int maxAge = int.MaxValue;
    List<UserInfo> result = new List<UserInfo>();
    foreach (UserInfo uInfo in _Users)
    {
        if (uInfo.Age >= minAge && uInfo.Age <= maxAge)
            result.Add(uInfo);
    }
    return result;
}
private List<UserInfo> FindUsers(int minAge, int maxAge)
{
    List<UserInfo> result = new List<UserInfo>();
    foreach (UserInfo uInfo in _Users)
    {
        if (uInfo.Age >= minAge && uInfo.Age <= maxAge)
            result.Add(uInfo);
    }
    return result;
}
Private Function GetUsersElderThen(minAge As Integer) As List(Of UserInfo)
    Dim maxAge As Integer = Integer.MaxValue
    Dim result As List(Of UserInfo) = New List(Of UserInfo)()
    For Each uInfo In _Users
        If uInfo.Age <= maxAge And uInfo.Age >= minAge Then
            result.Add(uInfo)
        End If
    Next
    Return result
End Function
Private Function FindUsers(minAge As Integer, maxAge As Integer) As List(Of UserInfo)
    Dim result As List(Of UserInfo) = New List(Of UserInfo)
    For Each uInfo In _Users
        If uInfo.Age <= maxAge And uInfo.Age >= minAge Then
            result.Add(uInfo)
        End If
    Next
    Return result
End Function

#Screenshot

CSharpInlineMethod