Skip to main content

Extract Method to Type

Creates a new method from the selected code block and moves it to the specified type, updating the selected block appropriately. The selection is replaced with suitable calling code to invoke the newly declared method through an instance of the target type.

#Availability

Available from the context menu or via shortcuts:

  • when a code block is selected.

#Example

public class ClassA 
{
    public string Name { get; set; }
}

public class ClassB  
{
    public int GetNameLength(ClassA o) 
    {return o.Name.Length;
    }
}
Public Class ClassA
    Public Name As String
End Class

Public Class ClassB
    Public Function GetNameLength(ByVal o As ClassA)Return o.Name.Length
    End Function
End Class

Result:

public class ClassA 
{
    public int GetNameLength() 
    {
        return Name.Length;
    }
    public string Name { get; set; }
}

public class ClassB  
{
    public int GetNameLength(ClassA o) 
    {
        return o.GetNameLength();
    }
}
Public Class ClassA
    Public Function GetNameLength() As Object
        Return Name.Length
    End Function
    Public Name As String
End Class

Public Class ClassB
    Public Function GetNameLength(ByVal o As ClassA)
        Return o.GetNameLength()
    End Function
End Class

#Screenshot

rsExtractMethodToType