No it doesn’t, generic methods that return T
always return the Type of T
as specified by the method signature. If T
is a subclass of the instance it will return the instance casted to T
.
So it does return Parent
which you can verify with the equivalent code below without using Type inference:
Parent parent = child.ConvertTo<Parent>();
Calling GetType()
returns the concrete type information, not the API return type that its currently cast to.
Again you can verify this by trying to call a child method on the parent instance:
public class Child : Parent
{
public void Hi() => Console.WriteLine("Hi");
}
var child = new Child();
var parent = child.ConvertTo<Parent>();
parent.Hi();
Which will fail with:
(17,8): error CS1061: ‘Parent’ does not contain a definition for ‘Hi’ and no accessible extension method ‘Hi’ accepting a first argument of type ‘Parent’ could be found (are you missing a using directive or an assembly reference?)
It’s returning the same instance because Parent
is already an instance of Child
, so no conversion is necessary.
The behavior you’re describing is addressed in my 2nd comment, The Convert<T>
API does not convert types it doesn’t have to convert, the comment also shows different cloning APIs you can use if you’re instead looking for shallow or deep copy semantics.