That's a safe cast in the context. You've created Derived
instance
that's why it's always safe to treat Derived as Derived; no GC
activity can spoil a part of the instance. Casting is a kind of
treatment (I'm going to treat actual Derived
istance as being
Base
only: I've promised to call just a subset of methods, properties)
and casting to itself is always safe.
// Actual instance is derived, but I'm going to restrict
// my work with them: I've promissed not to call for "b"
// any derived methods/properties
Base b = new Derived();
// What is the actual type of "b"?
String typeName = b.GetType().Name; // <- "Derived"
// Try to convert to Derived
// This's better way to cast then (Derived) b
Derived d = b as Derived;
// Since "b" is actually Derived d will ne not null
if (Object.RefrenceEquals(null, d)) {
Console.Write("b is Derived");
}
// You can also check
if (b is Derived) {
Console.Write("b is a Derived instance.");
}