I know there are already some questions posted related to this same topic, but I have seen different answers so I am quite confused on which answer is correct.
On the below link, people mentioned that downcasting is not possible Convert/Cast base type to Derived type
While on this link below, people mentioned that downcasting is possible only if the derived class is an instance of the base class downcast and upcast
I did a little experiment and implemented the downcasting on a mini project(C#) using direct casting, for example: DerivedClass dc = (DerivedClass) baseClass without checking whether or not DerivedClass is an instance of BaseClass, and to my surprise, it is working fine with no errors at all, so why people said downcasting is not possible?
So my question is, is downcasting really not possible? Why people seem to have different answers on this topic? after-all which one is the REAL answer? Could someone please explain this in more detail?
Sample code:
public class Order { }
public class PurchaseOrder : Order { // some new properties here for PurchaseOrder}
public static PurchaseOrder GetOrder()
{
Order order = new Order();
return (PurchaseOrder)order;
}