Encapsulate Downcast

Changes the return type of a method to the type that all callers downcast to, removing typecasting at calling sites.

Availability

Available from the context menu or via shortcuts:

  • when the caret is on a method declaration statement, provided that the method's return type is the ancestor of the method's return value. Note that the caret should be on the method name or on the return type name.

Example

private Object TestMethod()
{
    return new TestClass();
}
Private Function TestMethod() As Object
    Return New TestClass()
End Function

Result:

private TestClass TestMethod()
{
    return new TestClass();
}
Private Function TestMethod() As TestClass
    Return New TestClass()
End Function

Screenshot

rsEncapsulateDowncast